CPP STL Tutorial : C++ std::list and it’s operations.

In this chapter we shall learn about:

1. list introduction.
2. list declaration.
4. Passing std::list to function
5. list member function to iterate over elements.
6. list member function to check the capacity.
7. list member function to access the elements.
8. list member function to modify the elements.

1. list introduction.

1. List is an implementation of double linked list.
2. It allows constant time O(1) insertion and deletion of elements anywhere within the sequence.
3. When compared to vector, array and deque, list performs better in inserting, deleting and moving the elements.
4. The drawback is that they lack direct access to the elements by their position.

You need to use below header to use list:

#include <list>

2. list declaration.

A list can be declared as below:

list<int> mylist = {1, 2, 3};

4. Passing std::list to function

You can pass a deque in 2 ways:
1. Pass by value
2. Pass by reference

4.1. Pass by value

void func(list<int> list) 
{ 
   list.push_front(30); 
} 
   
int main() 
{ 
    list<int> list; 
    list.push_front(10); 
    list.push_front(20); 
   
    func(list); 
   
    // as we are passing by value, the list will remain unchanged.
	for ( auto it = list.begin(); it != list.end(); ++it )
    std::cout << ' ' << *it;

   
    return 0; 
} 

4.2. Pass by reference

void func(list<int> &myList) 
{ 
   myList.push_front(30); 
} 
   
int main() 
{ 
    list<int> myList; 
    myList.push_front(10); 
    myList.push_front(20); 
   
    func(myList); 
   
    // as we are passing by reference, the list will be changed.
	for ( auto it = myList.begin(); it != myList.end(); ++it )
    std::cout << ' ' << *it;
   
    return 0; 
} 

5. list member function to iterate over elements.

begin : It will return iterator to beginning
end : It will return iterator to end
rbegin : It will return reverse iterator to reverse beginning
rend : It will return reverse iterator to reverse end
cbegin : It will return const_iterator to beginning
cend   : It will return const_iterator to end
#include <iostream>
#include <list>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

   
int main() 
{ 
  list<int> mylist = {1, 2, 3, 4, 5};

  std::cout << "mylist contains:"<<endl;
  for (std::list<int>::iterator it=mylist.begin(); it != mylist.end(); ++it)
    std::cout << ' ' << *it;
   
  std::cout << "\nmylist backwards using rbegin and rend:"<<endl;
  for (std::list<int>::reverse_iterator rit=mylist.rbegin(); rit!=mylist.rend(); ++rit)
    std::cout << ' ' << *rit;
      
   std::cout << "\nmylist backwards using cbegin and cend:"<<endl;
   for (auto it = mylist.cbegin(); it != mylist.cend(); ++it)
   std::cout << ' ' << *it;
    return 0; 
} 
Output:
mylist contains:
 1 2 3 4 5
mylist backwards using rbegin and rend:
 5 4 3 2 1
mylist backwards using cbegin and cend:
 1 2 3 4 5

6. list member function to check the capacity.

empty : It will check whether container is empty.
size : It will return size.
max_size : It will return maximum size

7. list member function to access the elements.

front : It will access first element
back : It will access last element
#include <iostream>
#include <list>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

   
int main() 
{ 
  list<int> mylist = {1, 2, 3, 4, 5};

  std::cout << "mylist.front() is now " << mylist.front() << '\n';
  std::cout << "mylist.back() is now " << mylist.back() << '\n';

} 
Output:
mylist.front() is now 1
mylist.back() is now 5

8. list member function to modify the elements.

insert : It will insert elements
erase : It will erase elements
swap : It will swap content
resize : It will change size
clear : It will clear content
push_back : It will add element at the end
pop_back : It will delete last element
push_front : It will insert element at beginning
pop_front : It will delete first element
#include <iostream>
#include <list>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

   
int main() 
{ 
  list<int> mylist = {1, 2, 3, 4, 5};

  std::cout << "mylist.front() is now " << mylist.front() << '\n';
  std::list<int>::iterator it;
  it = mylist.begin();
  mylist.insert (it,10); 
  std::cout << "mylist.front() after insert() is now " << mylist.front() << '\n';

  mylist.push_front (300);
  std::cout << "mylist.front() after push_front() is now " << mylist.front() << '\n';

} 
Output:
mylist.front() is now 1
mylist.front() after insert() is now 10
mylist.front() after push_front() is now 300

9. list member function to perform Operations the elements.

splice : Transfer elements from list to list
remove : Remove elements with specific value
remove_if : Remove elements fulfilling condition (public member function template )
unique : Remove duplicate values
merge : Merge sorted lists
#include <iostream>
#include <list>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

   
int main() 
{ 
  list<int> mylist = {5, 4, 3, 2, 1, 1, 2};
  
  std::cout << "mylist contains:"<<endl;
  for (std::list<int>::iterator it=mylist.begin(); it != mylist.end(); ++it)
    std::cout << ' ' << *it;
   
  mylist.sort();
  
  std::cout << "\nmylist after sort contains:"<<endl;
  for (std::list<int>::iterator it=mylist.begin(); it != mylist.end(); ++it)
    std::cout << ' ' << *it;
    
     mylist.unique();           //  element is only removed from the list container if it compares equal to the element immediately preceding it. Thus, this function is especially useful for sorted lists.
  
  std::cout << "\nmylist after unique contains:"<<endl;
  for (std::list<int>::iterator it=mylist.begin(); it != mylist.end(); ++it)
    std::cout << ' ' << *it;
} 

Output:

mylist contains:
5 4 3 2 1 1 2
mylist after sort contains:
1 1 2 2 3 4 5
mylist after unique contains:
1 2 3 4 5
Write a Comment

Leave a Comment

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