Different Ways to Initialize a List in C++ STL
Last Updated :
30 May, 2023
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.
- Initializing an empty List and pushing values one by one
- Specifying List size and initializing all values
- Initializing List like the arrays
- Initializing a list from an array
- Initializing a list from a vector
- Initializing a list from another List
- Initializing the List using the fill() function
- 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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
list< int > li;
li.push_back(10);
li.push_back(20);
li.push_back(30);
for ( int x : li)
cout << x << " " ;
return 0;
}
|
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++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n = 4;
list< int > li(n,1);
for ( int x : li)
cout << x << " " ;
return 0;
}
|
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++
#include<bits/stdc++.h>
using namespace std;
int main()
{
list< int > li{ 10, 20, 30 };
for ( int x : li)
cout << x << " " ;
return 0;
}
|
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++
#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);
for ( int x : li)
cout << x << " " ;
return 0;
}
|
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++
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector< int > vect{ 10, 20, 30 };
list< int > li(vect.begin(), vect.end());
for ( int x : li)
cout << x << " " ;
return 0;
}
|
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++
#include<bits/stdc++.h>
using namespace std;
int main()
{
list< int > vect{ 10, 20, 30 };
list< int > li(vect.begin(), vect.end());
for ( int x : li)
cout << x << " " ;
return 0;
}
|
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++
#include<bits/stdc++.h>
using namespace std;
int main()
{
list< int > li(3);
int value = 10;
fill(li.begin(), li.end(), value);
for ( int x : li)
cout << x << " " ;
return 0;
}
|
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++; });
for ( const auto & element : myList) {
std::cout << element << " " ;
}
std::cout << std::endl;
return 0;
}
|
Output:
1 2 3 4 5
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...