C++ STL Forward List Container – std::forward_list

In this instructional exercise you will find out about C++ STL forward rundown i.e., std::forward_list and activities, techniques appropriate on it.

Forward Lists go under grouping compartments. Forward List executes independently connected rundown. Addition, expulsion and moving activities are exceptionally quick than different holders.

Each component of the forward list contains its next component address. Primary hindrance of the forward list is that can’t be iterated in reverse and its individual components can’t be gotten to straightforwardly.

At the point when separately connected rundown favoured over twofold connected rundown, Forward rundown is superior to List. Since list carries on a like twofold connected rundown.

Model for where we can use forward list is fastening in hashing, contiguousness list portrayal of the diagram and so on.

C++ STL Forward List

Presently how about we see what are the tasks we can apply on the forward rundown:

assign(): Just like addition strategy,  assign() will store the qualities. By utilizing assign () we can embed components in two different ways.

The first way is we can embed what are the components we required. The second way is we can embed a solitary component “n” number of times. In any case, in the second method for task rundown will be supplanted by new qualities. Old list esteems will be deleted.

Attempt this underneath code for all the more understanding:

#include<iostream>
#include<forward_list>
 
using namespace std;
 
int main(){
    forward_list<int> lst1; // declaration of a list
    forward_list<int> :: iterator it; // iterator for list
 
    // assigning values into list.
    lst1.assign({10,20,30});
 
    for(it = lst1.begin(); it!= lst1.end(); ++it)
        cout << *it << " ";
    
    cout << endl;
 
    // using assign() method in other way
    lst1.assign(3,99);
    // assigning three elements of each value 99
    
    for(it = lst1.begin(); it!= lst1.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
    
    return 0;
}

Output

10 20 30
99 99 99

Addition Functions

push_front(): This capacity used to embed new an incentive at the start of the rundown. The incentive from this capacity duplicated to the location before to the present first component of the compartment.

emplace_front(): This capacity likewise used to embed components at the start of the holder yet there no duplicate activity. Here component legitimately embeds at the location before to the principal component.

insert_after(): Using this capacity we can embed component at any situation of the forward rundown. The contentions which we go to this capacity will be duplicated to a specific area.

emplace_after(): This likewise works same as insert_after() work. Be that as it may, component legitimately embed with no duplicate activity.

Model program to appear above embed capacities:

#include<iostream>
#include<forward_list>
 
using namespace std;
 
int main(){
    forward_list <int> lst1;
    forward_list <int> :: iterator it;
    forward_list <int> :: iterator it1;
    
    lst1.assign({10,20,30}); // initially these 3 values are in forward list
 
    // using push front method
    lst1.push_front(5);
 
    // using emplace_front to insert at front
    lst1.emplace_front(4);
 
    // check the result
    for(it= lst1.begin(); it!= lst1.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
 
    // using insert_after function
    it1 = lst1.insert_after(lst1.begin(),99); // inserting 99 after current first element
 
    // using emplace_after function
    lst1.emplace_after(it1,0); // inserting 0 after which it1 iterator pointing to.
 
    // checking the result
    for(it = lst1.begin(); it!= lst1.end(); it++)
    	cout << *it << " ";
    
    cout << endl;
 
    return 0;
}

Output

4 5 10 20 30
4 99 0 5 10 20 30

Erase Functions

pop_front(): This capacity expels the principal component of the forward rundown.

erase_after(): This capacity used to erase the component at a specific situation of the forward rundown.

evacuate(): This capacity expels the particular component which we go as a contention to this capacity

remove_if(): This capacity evacuates the component we determined just the condition which we give is valid.

Model program to appear above eradicating elements of a forward list:

#include<iostream>
#include<forward_list>
 
using namespace std;
 
int main(){
    forward_list <int> lst1;
    forward_list <int> :: iterator it;
 
    lst1.assign({10,20,30,40,50});
    // initially we assigned some values into forward list
 
    // deleting first element
    lst1.pop_front();
 
    // deleting element at particular position using erase after function
    it = lst1.begin();
    lst1.erase_after(it);
 
    // check the result
    for(it = lst1.begin(); it!= lst1.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
 
    // deleting an element using remove function
    lst1.remove(50); // removing element 50 in forward list
 
    // deleting element by condition
    lst1.remove_if([](int a){ return a>25;});
 
    // check the result
    for(it = lst1.begin(); it!= lst1.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
    
    return 0;
}

Output

20 40 50
20

Some more capacities are:

splice_after(): This capacity used to move one forward rundown components to another forward rundown. It puts the components after the indicated position as it were.

turn around(): This capacity inverts all components of the forward rundown.

sort(): This capacity sorts the components in the forward rundown.

Model program to clarify the above capacities:

#include<iostream>
#include<forward_list>
 
using namespace std;
 
int main(){
    forward_list <int> lst1;
    forward_list <int> lst2;
    forward_list <int> :: iterator it;
    
    lst1.assign({20,40,60});
    lst2.assign({30,50,70});
 
    // now adding forward list1 elements to forward list2 using splice_after function after first element in this list
    lst2.splice_after(lst2.begin(),lst1);
 
    // Checking after adding forward list1 to forward list2
    cout << "after adding forward list1 into forward list2" << endl;
    for (it= lst2.begin(); it!= lst2.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
 
    lst2.reverse(); // preforming reverse operation on forward list2
 
    // printing revere elements
    cout << "after reversing forward list2" << endl;
    for (it= lst2.begin(); it!= lst2.end(); ++it)
    	cout << *it << " ";
   
    cout << endl;
 
    // sorting elements
    lst2.sort();
    cout << "after sorting the elements" << endl;
    for (it= lst2.begin(); it!= lst2.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
    
    return 0;
}

Output

after adding forward list1 into forward list2
30 20 40 60 50 70
after reversing forward list2
70 50 60 40 20 30
after sorting the elements
20 30 40 50 60 70

Remark underneath on the off chance that you have questions or discovered any data erroneous in above instructional exercise for C++ STL forward rundown.

Leave a Comment

error: Alert: Content is protected!!