C++ STL List Container – std::list

In this tutorial you will learn about C++ STL list container i.e. std::list and methods which can be applicable on it.

List comes under sequence containers. List stores elements in non-contiguous memory locations. List works same as double linked list. It can traverse in both directions. This is the reason list is slow in traversing when compared to vector. But it supports constant time insertion and removal of elements from anywhere in the container. If we want to implement single linked list then we should use forward list.

The main disadvantage by this list is, unlike other sequence containers elements of this container can’t be accessed by its index position.

C++ STL List

Declaring List

list<data_type> listName;

Operations Applicable on List Container

pus_front(x): It adds the new element ‘x’ at front of the list.

push_back(x): It adds the new element ‘x’ at the end of the list.

insert(): This function inserts the new elements to the list before the element at a specific position.

assign(): This erases the current elements of the list and adds new elements. Due to this replacement list size will change.

begin(): It returns the iterator pointing to the beginning of the list.

end(): This returns the iterator pointing to the last element of the list.

Example program to show ways to insert elements into list:

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

using namespace std;

int main(){
	list<int> lst1;
	list <int> :: iterator it;
	
	// inserting elements using push_front
	for(int i=0; i<3; i++)
	    lst1.push_front(i);
	
	// inserting elements using push_back
	for(int i=1; i<=3; i++)
	    lst1.push_back(i+10);
	
	// adding elements using insert() function
	it = lst1.begin();
	it++;
	lst1.insert(it, 34); // this inserts element 34 in front of iterator points
	
	// ohter way of adding elements using insert() method
	lst1.insert(it, 2, 44); // this inserts two elements of value 44 at where iterator points

	// displaying list
	for(it = lst1.begin(); it != lst1.end(); ++it)
	    cout << *it << " ";
    
    cout << endl;
	
	// this is adding elements using assign method
	lst1.assign(5,50);
	// this adds 5 elements of each value 50 by erasing all previous elements of the list. 
	
	// check again
	for(it = lst1.begin(); it != lst1.end(); ++it)
	    cout << *it << " ";
    
    cout << endl;
	
	return 0;
}

Output

2 34 44 44 1 0 11 12 13
50 50 50 50 50

Some more functions…

front(): It returns reference to the first element of the list.

back(): It returns reference to the current last element of the list.

pop_front(): This erases the first element of the list.

pop_back(): This erases the last element of the list.

erase(): It removes a single element or range of elements in from the list.

remove(x): It removes the all elements of the list which has value x .

empty(): This is Boolean type method. This returns whether the list is empty or not.

Example program to show all above functions:

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

using namespace std;

int main(){
	list <int> lst1;
	list <int> :: iterator it;
	list <int> :: iterator it1;
	
	// inserting some elements into list.
	for(int i=0; i<5; i++)
	    lst1.push_front(i+10);
	
	for(it =lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
	
	cout << endl;
	
	// getting front element
	cout << "the first element of the list is ";
	cout << lst1.front() << endl;
	
	// getting last element
	cout << "the last element of the list is ";
	cout << lst1.back() << endl;
	
	// erasing first element of the list
	lst1.pop_front();
	cout << "the first element after erasing current first elemnt is ";
	cout << lst1.front();
	cout << endl;
	
	// erasing last element of the list
	lst1.pop_back();
	cout << "the last element after erasing current last element is ";
	cout << lst1.back();
	cout << endl;
	
	// deleting elements using erase() function
	it = lst1.begin();
	lst1.erase(it); // this removes the element where itertaor points
	
	// displaying remaining elements in the list
	cout << "remaining elements after doing all above operations " << endl;
	for(it =lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
	
	cout << endl;
	
	// checking list is empty or not
	lst1.empty() ? cout << "List is empty" << endl : cout << "List is not empty" << endl;
	
	// applying remove() method
	lst1.remove(11);
	
	// displaying remaining elements in the list
	cout << "remaining elements after removing 11 are" << endl;
	for(it =lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
	
	cout << endl;
	
	return 0;
}

Output

14 13 12 11 10
the first element of the list is 14
the last element of the list is 10
the first element after erasing current first elemnt is 13
the last element after erasing current last element is 11
remaining elements after doing all above operations
12 11
List is not empty
remaining elements after removing 11 are
12

reverse(): This reverse all the elements of the list.

sort(): Sorts the all elements in the list in increasing order.

size(): This returns the number of elements in the list.

Example program to show above functions:

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

using namespace std;

int main(){
	list <int> lst1;
	list <int> :: iterator it;
	
	for(int i=0; i<6; i++){
		if(i%2) lst1.push_back(i);
		else lst1.push_front(i);
	}
	
	cout << "actual elements of the list are" << endl;
	for(it = lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
	
	cout << endl;
	
	// using reverse function
	lst1.reverse();
	cout << "Elements in the list after applying reverse operation " << endl;
	for(it = lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
	
	cout << endl;
	
	// using sort function
	lst1.sort();
	cout << "Elements in the list after applying sort operation" << endl;
	for(it = lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";

	cout << endl;
	
	// finding size
	cout << "size of the lis is ";
	cout << lst1.size();

	return 0;
}

Output

actual elements of the list are
4 2 0 1 3 5
Elements in the list after applying reverse operation
5 3 1 0 2 4
Elements in the list after applying sort operation
0 1 2 3 4 5
size of the lis is 6

Comment below if you have queries or found information incorrect in above tutorial for C++ STL List.

Leave a Comment

Your email address will not be published. Required fields are marked *