std::list example – C++ std::list Tutorial, Example and Usage Details

std::list example: In the previous article, we have discussed about C++ Map : Erase element by key or Iterator or Range. Let us learn std::list Tutorial, Example in C++ Program.

std::list

std::list is a sequence container, which is optimized for rapid insert and delete operations of elements anywhere from the list. It is usually implemented as a doubly-linked list. Here the elements are stored non-contiguously that’s why bidirectional iteration is possible here and it doesn’t support random access.

List Functions Use with programs :

How to use size()function:

size(): This function is used to return the size of the set container or the number of elements in the set container. Return value: it returns the number of elements in the set container.

#include <iostream>
#include <list>
using namespace std;
void print_list(list<int>& ll){
       for(list<int>::iterator it=ll.begin(); it != ll.end(); ++it)
          cout << *it << "\t";
    cout << endl;
}
int main(){

  list<int> nums = {1,2,3,4,5};
   print_list(nums);
 cout << "Size = "<< nums.size() << endl;
 list<int> nums2 = nums;
 print_list(nums2); 
 return 0;
}
Output:

1 2 3 4 5 
Size = 5
1 2 3 4 5

How to implement front(),back() and empty()function:

front(): It is a built-in function in c++ STL that is used to return a  reference to the first element in the list container. This function returns a direct reference to the first element in the list container.

back(): This function in c++ STL returns a direct reference to the last element in the list container.

empty(): It is a built-in function in c++ STL that is used to check whether a particular list container is empty or not. This function does not modify the list, it simply checks whether a list is empty or not.

#include <iostream>
#include <list>
using namespace std;
void print_list(list<int>& ll){
       for(list<int>::iterator it=ll.begin(); it != ll.end(); ++it)
          cout << *it << "\t";
        cout << endl;
}
int main()
{
  list<int> nums = {1,2,3,4,5};
print_list(nums);
cout << "Size = " << nums.size() << endl;;
list<int> nums2 = nums;
print_list(nums2) ;
cout <<"front = " << nums.front() <<", back = " << nums.back() << endl;
cout << nums.empty() << endl;
return 0;
}

Output :

1 2 3 4 5 
Size = 5
1 2 3 4 5 
front = 1, back = 5
0

How to use begin(), rbegin(), rend() and end() function:

begin(): This function is used to return an iterator pointing to the first element of the vector container and this function returns a bidirectional iterator to the first element of the container.

rbegin(): It is a built-in function in C++ STL that returns a reverse iterator pointing to the last element in the container.

rend(): It is a built-in function in C++ STL that returns a reverse iterator pointing to the theoretical element right before the first element in the array container.

end(): It is a built-in function in C++ STL that is used to get an iterator to past the last element. By past the last element it is meant that the iterator returned by the end() function returns an iterator to an element that follows the last element in the list container.

#include <iostream>
#include <list>
using namespace std;
void print_list(list<int>& ll){
       for(list<int>::iterator it=ll.begin(); it != ll.end(); ++it)
          cout << *it << "\t";
        cout << endl;
}
int main()
{
  list<int> nums = {1,2,3,4,5};
print_list(nums);
cout << "Size = " << nums.size() << endl;;
list<int> nums2 = nums;
print_list(nums2) ;
cout <<"front = "<< nums.front() <<", back = " << nums.back() << endl;
cout << nums.empty() << endl;
cout << *nums.begin() << "," << *nums.rbegin() <<endl;
return 0;
}
Output :

1 2 3 4 5 
Size = 5
1 2 3 4 5 
front = 1, back = 5
0
1,5

How to insert element at front , middle and end of the list :

#include <algorithm>
#include <iostream>
#include <list>
 int main(){
    std::list<int> l = { 7, 5, 16, 8 };
 
    
    l.push_front(25);
    l.push_back(13);
    auto it = std::find(l.begin(), l.end(), 16);
    if (it != l.end()) {
        l.insert(it, 42);
    }
    std::cout << "l = { ";
    for (int n : l) {
        std::cout << n << ", ";
    }
    std::cout << "};\n";
}
Output :

l = { 25, 7, 5, 42, 16, 8, 13, };