We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Different ways to Initialize a list in C++

In this article we will discuss the different ways to initialize a std::list in C++.


std::list provides various overloaded constructors for creation and initialization of list. Let’s see different ways to create & initialize a std::list in C++ i.e.

Advertisements

Creating an Empty List in C++

std::list has a default constructor that will create an empty list i.e.

explicit list (const allocator_type& alloc = allocator_type());
// Create an empty list of ints
std::list<int> listOfInts;

Let’s see an example to create an empty list and the push elements in it i.e

#include <iostream>
#include <list>

int main() {
	// Create an empty list of ints
	std::list<int> listOfInts;

	// Push back 10 elements in the list
	for (int i = 0; i < 10; i++)
		listOfInts.push_back(i);

	// Iterate over the list and display numbers
	for (int val : listOfInts)
		std::cout << val << ",";
	std::cout << std::endl;

	return 0;
}

Output:

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

As above example contains c++11 range based for loop, so to compile the above example use following command,

g++ --std=c++11 example1.cpp

Creating & Initializing a List with Fill Constructor

In this example we will create a List using its Fill Constructor i.e.

list (size_type n, const value_type& val,const allocator_type& alloc = allocator_type());
std::list<int> listOfInts(5, 119);

It will create a list with 5 elements and each is initialized with a copy of element passed as second argument i.e. 119 in our case.

Example of initializing a list with Fill Constructor is as follows,

#include <iostream>
#include <list>

int main() {
	// Create a list and initialize it with 5 elements of value 119
	std::list<int> listOfInts(5, 119);

	// Iterate over the list and display numbers
	for (int val : listOfInts)
		std::cout << val << ",";
	std::cout << std::endl;

	return 0;
}

Output:

119,119,119,119,119,

As above example contains c++11 range based for loop, so to compile the above example use following command,

g++ --std=c++11 example2.cpp

Creating & Initializing a List with c++11’s initializer_list

In C++11 new overloaded constructor of std::list is introduced that can receive an initializer_list as argument and can initialize list elements with a copy of elements in initializer_list i.e.

list (initializer_list<value_type> il,const allocator_type& alloc = allocator_type());
std::list<int> listOfInts({2,8,7,5,3,1,4});

[showads ad=inside_post]

 

Lets see an example to initialize a std::list with an initialzer_list i.e.

#include <iostream>
#include <list>
#include <list>

int main() {
	// Create a list and initialize it initializer_list of 7 elements
	std::list<int> listOfInts( { 2, 8, 7, 5, 3, 1, 4 });

	// Iterate over the list and display numbers
	for (int val : listOfInts)
		std::cout << val << ",";
	std::cout << std::endl;

	// Create a initializer list of strings
	std::initializer_list<std::string> initLits =
			{ "Hi", "this", "is", "sample" };

	// Create & Initialize a list with initializer_list object
	std::list<std::string> listOfStrs(initLits);

	// Iterate over the list and display strings
	for (std::string data : listOfStrs)
		std::cout << data << std::endl;

	return 0;
}

Output:

2,8,7,5,3,1,4,
Hi
this
is
sample

As above example contains c++11 range based for loop, so to compile the above example use following command,

g++ --std=c++11 example3.cpp

Initializing a std::list with a std::vector or an Array

std::list provides an over loaded version of constructor that accepts a range i.e.

 list (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());

Let’s use this to initialize a std::list with a Vector i.e.

#include <iostream>
#include <list>
#include <vector>

int main() {
	std::vector<int> vecOfInt( { 2, 8, 7, 5, 3, 1, 4 });

	// Create a list and initialize it with vector
	std::list<int> listOfInts(vecOfInt.begin(), vecOfInt.end());

	// Iterate over the list and display numbers
	for (int val : listOfInts)
		std::cout << val << ",";
	std::cout << std::endl;

	return 0;
}

Initializing a std::list from an array using above mentioned constructor,

#include <iostream>
#include <list>
#include <vector>

int main() {
	int arr[] = { 2, 8, 7, 5, 3, 1, 4 };

	// Create a list and initialize it with vector
	std::list<int> listOfInts(arr, arr + sizeof(arr) / sizeof(int));

	// Iterate over the list and display numbers
	for (int val : listOfInts)
		std::cout << val << ",";
	std::cout << std::endl;

	return 0;
}

Output of above 2 Examples:

2,8,7,5,3,1,4,

As above example contains c++11 range based for loop, so to compile the above example use following command,

g++ --std=c++11 example4.cpp

Cart

Your Cart is Empty

Back To Shop