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

In this chapter we shall learn about:

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

1. forward_list introduction.

1. forward_list is implementation of single linked list.
2. forward_list is useful in case of insertion, extraction and moving the elements inside the container.
3. They are stored in unrelated storage locations.
4. The major disadvantage of “forward_list” is that, they lack direct access to the elements by their position. If you want to go to 5th element, then you have to iterate from the beginning to that position.

Below is the header file to be used for forward_list

#include <forward_list>

2. forward_list declaration.

Below is the way you can declare forward_list:

forward_list<int> myforwardlist{ 1, 2, 3, 4, 5 }; 

4. Passing std::forward_list to function

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

4.1. Pass by value

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

   
    return 0; 
} 

4.2. Pass by reference

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

5. forward_list member function to iterate over elements.

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

int main ()
{
  std::forward_list<int> mylist = {20, 30, 40, 50};

  std::cout << "mylist contains:"<<endl;
  for ( int& x: mylist ) std::cout << ' ' << x;
  std::cout << '\n';


  mylist.insert_after ( mylist.before_begin(), 11 );

  std::cout << "mylist contains after inseting in begining :"<<endl;
  for ( int& x: mylist ) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}
Output:
mylist contains:
 20 30 40 50
mylist contains after inseting in begining :
 11 20 30 40 50

6. forward_list member function to check the capacity.

empty : Test whether array is empty
max_size : Return maximum size

7. forward_list member function to access the elements.

push_front:  access the first element
#include <iostream>
#include <forward_list>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main ()
{
  std::forward_list<int> mylist = {2, 16, 77};

  mylist.front() = 11;

  std::cout << "mylist now contains:";
  for ( int& x : mylist ) std::cout << ' ' << x;

  std::cout << '\n';

  return 0;
}
Output:
mylist now contains: 11 16 77

8. forward_list member function to modify the elements.

assign : Assign content
emplace_front : Construct and insert element at beginning
push_front : Insert element at beginning
pop_front : Delete first element
emplace_after : Construct and insert element
insert_after : Insert elements
erase_after : Erase elements
swap : Swap content
resize : Change size
clear : Clear content
#include <iostream>
#include <forward_list>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main ()
{
  forward_list<int> mylist = {1, 2, 3};
  
  std::cout << "mylist contains:"<<endl;
  for (int& x: mylist) std::cout << ' ' << x;
  std::cout << '\n';
  
  mylist.push_front (4);
  mylist.push_front (5);

  std::cout << "mylist contains after push_front:"<<endl;
  for (int& x: mylist) std::cout << ' ' << x;
  std::cout << '\n';
    
  mylist.pop_front();
  
  std::cout << "mylist contains after pop_front:"<<endl;
  for (int& x: mylist) std::cout << ' ' << x;
  std::cout << '\n';
  
 std::forward_list<int>::iterator it;
  it = mylist.insert_after ( mylist.before_begin(), 10 );          // 10
                                                                   //  ^  <- it
  it = mylist.insert_after ( it, 2, 20 );                          // 10 20 20
  
  std::cout << "mylist contains after before_begin:"<<endl;
  for (int& x: mylist) std::cout << ' ' << x;
  std::cout << '\n';
   
}

Output:

mylist contains:
1 2 3
mylist contains after push_front:
5 4 1 2 3
mylist contains after pop_front:
4 1 2 3
mylist contains after before_begin:
10 20 20 4 1 2 3
Write a Comment

Leave a Comment

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