Open In App

Different Ways to Initialize a List in C++ STL

Last Updated : 30 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: List in C++

Lists are sequence containers that allow non-contiguous memory allocation The following are the different ways to create and initialize a List in C++ STL.

  1. Initializing an empty List and pushing values one by one
  2. Specifying List size and initializing all values
  3. Initializing List like the arrays
  4. Initializing a list from an array
  5. Initializing a list from a vector
  6. Initializing a list from another List
  7. Initializing the List using the fill() function
  8. Using a lambda expression and the generate() function:

1. Initializing an empty List and pushing values one by one

The standard way to initialize a list is to first create an empty list and then elements are added to that list using the inbuilt list_name.push_back() method.

Syntax:

list<data_type> li;

li.push_back(ele);

// Here list li is initialized and element ele is inserted into that list.

Example:

C++




// C++ program to create an empty List
// and push values one by one.
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Create an empty List
    list<int> li;
 
    // Adding values to the List
    li.push_back(10);
    li.push_back(20);
    li.push_back(30);
     
     
    // Printing the List
    for (int x : li)
        cout << x << " ";
 
    return 0;
}


Output

10 20 30 

2. Specifying List size and initializing all values

Another way to initialize a list is by assigning a particular value to all the elements of the list. In this method, we pass a desired size for the list and the value to be stored for all the elements of that size.

Syntax:

list<data_type> li(n,val);
// Here list li is initialized with size as n and values as val.

Example:

C++




// C++ program to specifying size
// and initializing all values in list
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    int n = 4;
    // Create a list of size n with
    // all values as 1.
    list<int> li(n,1);
     
    // Printing the List
    for (int x : li)
        cout << x << " ";
 
    return 0;
}


Output

1 1 1 1 

3. Initializing List like the arrays 

Another way of initialization is by passing a predetermined list of elements (initializer list) as an argument. The syntax for this method is shown below:

Syntax:

list<data_type> li{element1,element2,element3,element4};

Example:

C++




// C++ program to initialise the List
// and inserting values directly
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    // Initialising the List
    // with values
    list<int> li{ 10, 20, 30 };
     
    // Printing the List
    for (int x : li)
        cout << x << " ";
 
    return 0;
}


Output

10 20 30 

4. Initializing a list from an array

One can also initialize the list from an array with the same data type.

Syntax:

list<data_type>li(old_array,old_array+size); 

Here old_array is the array containing elements of the same data type as mentioned in the declaration of the list and size represents the length till which we want to copy the elements from an array to the list.

Below is the C++ program to implement the above approach:

C++




// C++ program to initialize the List
// from array
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    int arr[] = { 10, 20, 30 };
    int n = sizeof(arr) / sizeof(arr[0]);
   
    list<int> li(arr, arr + n);
     
    // Printing the List
    for (int x : li)
        cout << x << " ";
 
    return 0;
}


Output

10 20 30 

5. Initializing a list from a vector

The list can also be initialized using a vector of the same data type. We provide the iterators of that vector as arguments in order to copy the elements.

Syntax:

list<data_type>li(old_vector.begin(),old_vector.end()); 

Here old_vector is a vector whose elements are copied in list li.

Below is the C++ program to implement the above approach:

C++




// C++ program to initialize the List
// from vector
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    vector<int> vect{ 10, 20, 30 };
   
    list<int> li(vect.begin(), vect.end());
     
    // Printing the List
    for (int x : li)
        cout << x << " ";
 
    return 0;
}


Output

10 20 30 

6. Initializing from another List

Another way to initialize a list is to use another list. In this, we provide iterators of the old list as arguments. The syntax for this is given below.

Syntax:

list<data_type>li(old_list.begin(),old_list.end()); 

Example:

C++




// C++ program to initialize the List
// from another List
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    // Initialising first list
    list<int> vect{ 10, 20, 30 };
    
    // Initialising the second list
    // from the first list
    list<int> li(vect.begin(), vect.end());
     
    // Printing the second List
    for (int x : li)
        cout << x << " ";
 
    return 0;
}


Output

10 20 30 

7. Initializing the List using the fill() function

One can also initialize a list using the fill() function in C++. The ‘fill’ function assigns any particular value to all the elements in the given range. Here the range is provided with the help of iterators.

Syntax:

list<data_type> li(n);
fill(li.begin(), li.end(), val);

// Here, the list li is initialized with n as size and the value of all elements as val.

Example:

C++




// C++ program to initialize the List
// using fill function
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    // Initialising the list
    list<int> li(3);
    int value = 10;
    fill(li.begin(), li.end(), value);
     
    // Printing theList
    for (int x : li)
        cout << x << " ";
 
    return 0;
}


Output

10 10 10 

8. Using a lambda expression and the generate() function:

Create and initialize a list in C++ STL with a lambda expression and the generate() function. The lambda expression captures variables and defines the logic for   generating values. By applying the generate() function to the list range, the generated values are to be assigned to the respective list of elements. This approach provides a concise and efficient way to populate a list with custom-generated values.

Syntax 

std::generate(list.begin(), list.end(), [&]() { /* lambda expression */ });

Example 

C++




#include <iostream>
#include <list>
#include <algorithm>
 
int main() {
    std::list<int> myList(5);
    int value = 1;
    std::generate(myList.begin(), myList.end(), [&value]() { return value++; });
 
    // Print the contents of the list
    for (const auto& element : myList) {
        std::cout << element << " ";
    }
    std::cout << std::endl;
 
    return 0;
}


Output:

1 2 3 4 5


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads