In this article, we will learn different ways to Initialize a list in C++ language.std::list provides us with the implementation of list data structure in C++. This is part of the STL std library. The different ways that we are going to learn in this article are:
Different ways to Initialize a list in C++
- initialize a list using the empty list and push_back() function.
- initialize a list using the Fill Constructor.
- initialize a list by using the Initializer list.
1. By using an Empty list and push_back() function
In this first example, we are going to create an empty list and then we will use the push_back() function to insert elements one by one. Let us understand this with the below code example.
#include <iostream>
#include <list>
using namespace std;
int main()
{
std::list<int> Numlist;
for (int i = 1; i <= 10; i++)
Numlist.push_back(i);
std::cout << "The elements in the List are: ";
for (int elem : Numlist)
std::cout << elem << ",";
std::cout << std::endl;
return 0;
}
Output
The elements in the List are: 1,2,3,4,5,6,7,8,9,10
2. Initialize a list in C++ By using Fill Constructor
The second example, demonstrate the use of a fill constructor to initialize a list with some elements. In this example, we are doing the declaration and initialization in one line itself by using the fill constructor. To use the fill constructor, We pass the size of the list and the element which we want to fill in all elements of the list.
Let us understand this with the code example below:
#include <iostream>
#include <list>
using namespace std;
int main()
{
std::list<int> Numlist(10, 1);
std::cout << "The elements in the List are: ";
for (int elem : Numlist)
std::cout << elem << ",";
std::cout << std::endl;
return 0;
}
Output
The elements in the List are: 1,1,1,1,1,1,1,1,1,1,
3. Initialize a list in C++ By using the Initializer list
The third method that we are going to learn is by using the Initializer list of elements. This method looks very similar in Syntax to the one we just learned above. But the logic behind this is different. With the introduction of C++ 11, we got the support of the initializer_list concept. This concept allows us to provide the elements of a list that we want to initialize to this list. Let us see the use of initializer_list in the below code example.
#include <iostream>
#include <list>
using namespace std;
int main()
{
std::list<int> Numlist( { 1, 2, 3, 4, 5, 6, 7 });
std::cout << "The elements in the List are: ";
for (int elem : Numlist)
std::cout << elem << ",";
std::cout << std::endl;
return 0;
}
Output
The elements in the List are: 1,2,3,4,5,6,7,
4. Initialize a list in C++ a list of strings
In this example we have used initializer_list to intailize a list of string in C plus plus.Let us understand with below example.
#include <iostream>
#include <list>
using namespace std;
int main()
{
std::initializer_list<std::string> Strlist =
{ "Sachin", "Dravid", "Lara", "Dhoni" };
std::list<std::string> Playerlist(Strlist);
std::cout << "The Players in the List are: ";
for (auto elem : Playerlist)
std::cout << elem << std::endl;
return 0;
}
Output
The Players in the List are: Sachin
Dravid
Lara
Dhoni