Different ways to Initialize a list in C++

In the previous article, we have discussed about C++ List – Find | Contains : How to search an element in std::list ?. Let us learn how to Initialize a list in C++ Program.

In this article we are going to see the different types of methods in which we can initialize a std::list in C++.

Let’s see one by one.

Method-1 : Creating an Empty List in C++

To create a list we can use the default constructor provided by the std::list.

Below code is the implementation of this.

#include <iostream>
#include <list>
int main()
{
    //Creating an empty integer list
    std::list<int> listInt;
    //Pushing 10 elements into the list
    for (int i = 10; i >= 1; i--)
        listInt.push_back(i);
    //Show all the elements
    for (int value : listInt)
        std::cout << value << ",";
    std::cout << std::endl;
    return 0;
}
Output:

10,9,8,7,6,5,4,3,2,1,

Method-2 : Creating & Initializing a List with Fill Constructor

In this example we are going to create the list using fill constructor.

#include <iostream>
#include <list>
int main()
{
    //Creating a list of 10 elements having value 123
    std::list<int> listInt(10, 123);
    //Print all the elements
    for (int value : listInt)
        std::cout << value << ",";
    std::cout << std::endl;
    return 0;
}
Output :

123,123,123,123,123,123,123,123,123,123,

Method-3 : Creating & Initializing a List with c++11’s initializer_list

Below code is the implementation of this.

#include <iostream>
#include <list>
int main()
{
    //Creating a list of 5 elements having value 123
    std::list<int> listInt(5, 123);
    //Print all the elements
    for (int value : listInt)
        std::cout << value << ",";
    std::cout << std::endl;

    // Creating a string initializer
    std::initializer_list<std::string> initList =
        {"Hi", "this", "is", "an", "example"};
    // Create & Initialize a list with objects from the initializer list
    std::list<std::string> stringList(initList);
    // Displaying the string list
    for (std::string data : stringList)
        std::cout << data << std::endl;

    return 0;
}
Output :

123,123,123,123,123,

Hi

this

is

an

example

Method-4 : Initializing a std::list with a std::vector or an Array

We can also pass a range into the constructor.

Below code is the implementation of this.

#include <iostream>
#include <list>
#include <vector>
int main()
{
    std::vector<int> vec({
        5,
        3,
        1,
        5,
        9,
        7,
        6,
    });
    // Creating and initializing a list using vector
    std::list<int> listInt(vec.begin(), vec.end());

    // Creating and initializing a list using array
    int arr[] = {9, 3, 6, 7, 8, 5, 2};
    std::list<int> listIntArr(arr, arr + sizeof(arr) / sizeof(int));
    // Display all the elements initialized by the vector
    std::cout << "Using Vector" << std::endl;
    for (int value : listInt)
        std::cout << value << ",";
    std::cout << std::endl;

    // Display all the elements initialized by the array
    std::cout << "Using Array" << std::endl;
    for (int value : listIntArr)
        std::cout << value << ",";
    std::cout << std::endl;

    return 0;
}
Output :

Using Vector

5,3,1,5,9,7,6,

Using Array

9,3,6,7,8,5,2