What is a List?
A list in STL is a contiguous container that allows the inserting and erasing of elements in constant time and iterating in both directions.
Linked list can be implemented by using the list container. Array and Vector are contiguous containers, they store their data on continuous memory, thus the insert operation at the middle of vector/array is very costly (in terms of number of operation and process time) because we have to shift all the elements, linked list overcome this problem.
List are part of the C++ Standard Template Library. To use vectors, we need to include the List header file in our program.
#include <list>
Syntax:
std::list<object_type> variable_name;
Example:
list<int> li; list<string> li;
Most used functions in list::
- push_back() – to insert an element at the end of the list.
list<int> li; li.push_back(110); li.push_back(220);
- push_front() – to insert an element at the front of the list.
list<int> li; li.push_front(110); li.push_front(220);
- pop_back() – deletes the last element of the list.
li.pop_back();
- pop_front() – deletes the front element of the list.
li.pop_front();
- front() – it gives a reference to the first element of the list.
li.front();
- back() – it gives a reference to the last element of the list.
li.back();
- reverse() – reverse the list.
li.reverse();
- sort() – sorts the list in ascending order.
li.sort();
- size() – returns the number of elements on the list.
li.size();
- empty() – to check if the list is empty or not.
li.empty();
- push_front(g) – to insert a new element ‘g’ at the beginning of the list.
list<int> li = {1, 2, 3, 4, 5}; li.push_front(34);
- push_back(g) – to insert a new element ‘g’ at the end of the list.
list<int> li = {1, 2, 3, 4, 5}; li.push_back(14);
- assign() – Assigns new elements to list by replacing current elements and resizes the list.
// declaring list list<int> li; // Assigning the value 100, 5 times // to the list, li. li.assign(5, 100); {100, 100, 100, 100, 100}
- insert() – Inserts new elements in the list before the element at a specified position.
// declaring list list<int> li1; // using assign() to insert multiple numbers // creates 3 occurrences of "2" li1.assign(3, 2); //{2, 2, 2} // using insert to insert 1 element at the last position // inserts 5 at last position li1.insert(li1.end(), 5); //{2, 2, 2, 5}
- erase() – Removes a single element or a range of elements from the list.
// declaring list list<int> li1; // using assign() to insert multiple numbers // creates 3 occurrences of "2" li1.assign(3, 2); //{2, 2, 2} li1.push_back(5); li1.push_back(6); //{2, 2, 2, 5, 6} li1.erase(li1.begin()); //{2, 2, 5, 6}
- remove() – Removes all the elements from the list, which are equal to given element.
// declaring list list<int> li; // Add elements to the List li.push_back(10); li.push_back(20); li.push_back(20); li.push_back(30); li.push_back(40); // delete all elements with value 20 li.remove(20); //{10, 30, 40}
Other Functions:
- max_size() – Returns the maximum number of elements a list container can hold.
- unique() – Removes all duplicate consecutive elements from the list.
- emplace_front() and emplace_back() – emplace_front() function is used to insert a new element into the list container, the new element is added to the beginning of the list. emplace_back() function is used to insert a new element into the list container, the new element is added to the end of the list.
- clear() – This function is used to remove all the elements of the list container, thus making it size 0.
- swap() – This function is used to swap the contents of one list with another list of same type and size.
- splice() – This function is used to transfer elements from one list to another.
- merge() – Merges two sorted lists into one.
- emplace() – Extends list by inserting new element at a given position.
Example with Code :
//header files #include <iostream> #include<iterator> #include <list> using namespace std; //print the list void printlist(list<int> li) { list<int>::iterator it; for (it = li.begin(); it != li.end(); it++) { cout << *it << " "; } cout << endl; } int main() { list<int> li; li.push_back(10); li.push_back(20); li.push_front(30); li.push_front(40); li.push_front(50); //{50, 40, 30, 10, 20} cout << "The elements in the list are: "; printlist(li); cout << "Reversing the list: "; li.reverse(); printlist(li); //{20, 10, 30, 40, 50} cout << "Sorting the list: "; li.sort(); printlist(li); //{10, 20, 30, 40, 50} cout << "The size of the list is: " << li.size() << endl; cout << "The first element in the list: " << li.front() << endl; cout << "Deleting the first element" << endl; li.pop_front(); printlist(li); cout << "The last element of the list: " << li.back() << endl; cout << "Deleting the last element" << endl; li.pop_back(); printlist(li); return 0; }
Output:

- begin() – begin() function returns an iterator pointing to the first element of the list.
- end() – end() function returns an iterator pointing to the theoretical last element which follows the last element.
- rbegin() – rbegin() returns a reverse iterator which points to the last element of the list.
- rend() – rend() returns a reverse iterator which points to the position before the beginning of the list.
- cbegin() – cbegin() returns a constant random access iterator which points to the beginning of the list.
- cend() – cend() returns a constant random access iterator which points to the end of the list.
- crbegin() – crbegin() returns a constant reverse iterator which points to the last element of the list i.e reversed beginning of container.
- crend() – crend() returns a constant reverse iterator which points to the theoretical element preceding the first element in the list i.e. the reverse end of the list.
Example with Code :
#include <iostream> #include <list> using namespace std; int main() { // declaration of list container list<int> mylist{1, 2, 3, 4, 5}; // using begin() to print list cout<<"Print list elements: "<<endl; for (auto it = mylist.begin(); it != mylist.end(); ++it) cout << ' ' << *it; cout << endl; cout << "The list in reverse order: "; for (auto it = mylist.rbegin(); it != mylist.rend(); ++it) cout << *it << " "; cout << endl; // Prints the first element cout << "The first element is: " << *mylist.cbegin(); // printing list elements cout << "\nList: "; for (auto it = mylist.cbegin(); it != mylist.end(); ++it) cout << *it << " "; // prints the last element cout << "The last element is: " << *mylist.crbegin(); cout << "\nList: "; for (auto it = mylist.crbegin(); it != mylist.crend(); ++it) cout << *it << " "; cout<<endl; //insert 5 at the last position mylist.insert(mylist.end(), 5); // Printing the new list cout << "The list after inserting" << " 1 element using insert() is : "; for (list<int>::iterator i = mylist.begin(); i != mylist.end(); i++) cout << *i << " "; cout << endl; // using insert to insert // 2 element at the last position // inserts 2 occurrences // of 7 at last position mylist.insert(mylist.end(), 2, 7); // Printing the new list cout << "The list after inserting" << " multiple elements " << "using insert() is : "; for (list<int>::iterator i = mylist.begin(); i != mylist.end(); i++) cout << *i << " "; cout << endl; }
Output:

Example with Code :
#include <iostream> #include <list> using namespace std; int main() { // list declaration list<int> list1{1, 2, 3, 4}; list<int> list2{3, 5, 7, 9}; // checking the max size of the list cout << "max size: " << list1.max_size() << endl; // using swap() function to // swap elements of lists list1.swap(list2); // printing the first list cout << "list1 = "; for (auto it = list1.begin(); it != list1.end(); ++it) cout << ' ' << *it; cout << endl; // printing the second list cout << endl << "list2 = "; for (auto it = list2.begin(); it != list2.end(); ++it) cout << ' ' << *it; cout << endl; return 0; }

Example with Code :
#include <iostream> #include <list> using namespace std; int main() { list<int> list1 = {11, 22, 93}; list<int> list2 = {14, 51}; list<int> list3 = {26, 27, 18}; // transfer all the elements of l2 list1.splice(list1.begin(), list2); // at the beginning of l1 cout << "list l1 after splice operation" << endl; for (auto x : list1) cout << x << " "; cout << endl; // transfer all the elements of l1 list3.splice(list3.end(), list1); // at the end of l3 cout << "\nlist l3 after splice operation" << endl; for (auto x : list3) cout << x << " "; cout << endl; return 0; }

To master C++ Standard Template Library (STL) in the most efficient and effective way, do check out this:
This article is contributed by : Sayan Dutta to this article on csforall.in
[…] list […]