C++ STL List Container – std::list

In this instructional exercise, you will find out about the C++ STL list compartment, for example, std:: list and strategies which can be relevant to it.

The rundown goes under arrangement holders. Rundown stores components in non-bordering memory areas. Rundown works are same as a twofold connected rundown. It can cross in the two bearings.

This is the explanation list is delayed in crossing when contrasted with vector. Be that as it may, it bolsters steady time inclusion and expulsion of components from anyplace in the holder.

On the off chance that we need to actualize single connected rundown, at that point we should use forward rundown.

The principle detriment by this rundown is, not normal for other arrangement compartments components of this holder can’t be gotten to by its list position.

C++ STL List

Announcing List

list<data_type> listName;

Activities Applicable on List Container

pus_front(x): It includes the new component ‘x’ at front of the rundown.

push_back(x): It includes the new component ‘x’ toward the finish of the rundown.

embed(): This capacity embeds the new components to the rundown before the component at a particular position.

dole out(): This eradicates the present components of the rundown and includes new components. Because of this substitution list size will change.

start(): It restores the iterator indicating the start of the rundown.

end(): This profits the iterator indicating the last component of the rundown.

Model program to demonstrate approaches to embed components into the list:

#include<iostream>
#include<list>
#include<iterator>
 
using namespace std;
 
int main(){
	list<int> lst1;
	list <int> :: iterator it;
	
	// inserting elements using push_front
	for(int i=0; i<3; i++)
	    lst1.push_front(i);
	
	// inserting elements using push_back
	for(int i=1; i<=3; i++)
	    lst1.push_back(i+10);
	
	// adding elements using insert() function
	it = lst1.begin();
	it++;
	lst1.insert(it, 34); // this inserts element 34 in front of iterator points
	
	// ohter way of adding elements using insert() method
	lst1.insert(it, 2, 44); // this inserts two elements of value 44 at where iterator points
 
	// displaying list
	for(it = lst1.begin(); it != lst1.end(); ++it)
	    cout << *it << " ";
    
    cout << endl;
	
	// this is adding elements using assign method
	lst1.assign(5,50);
	// this adds 5 elements of each value 50 by erasing all previous elements of the list. 
	
	// check again
	for(it = lst1.begin(); it != lst1.end(); ++it)
	    cout << *it << " ";
    
    cout << endl;
	
	return 0;
}

Output

2 34 44 44 1 0 11 12 13
50 50 50 50 50

Some more capacities…

front(): It returns a reference to the main component of the rundown.

back(): It returns a reference to the present last component of the rundown.

pop_front(): This eradicates the main component of the rundown.

pop_back(): This deletes the last component of the rundown.

delete(): It expels a solitary component or scope of components in from the rundown.

remove(x): It expels all components of the rundown which has esteem x.

void(): This is a Boolean type strategy. This profits whether the rundown is unfilled or not.

Model program to show every single above work:

#include<iostream>
#include<list>
#include<iterator>
 
using namespace std;
 
int main(){
	list <int> lst1;
	list <int> :: iterator it;
	list <int> :: iterator it1;
	
	// inserting some elements into list.
	for(int i=0; i<5; i++)
	    lst1.push_front(i+10);
	
	for(it =lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
	
	cout << endl;
	
	// getting front element
	cout << "the first element of the list is ";
	cout << lst1.front() << endl;
	
	// getting last element
	cout << "the last element of the list is ";
	cout << lst1.back() << endl;
	
	// erasing first element of the list
	lst1.pop_front();
	cout << "the first element after erasing current first elemnt is ";
	cout << lst1.front();
	cout << endl;
	
	// erasing last element of the list
	lst1.pop_back();
	cout << "the last element after erasing current last element is ";
	cout << lst1.back();
	cout << endl;
	
	// deleting elements using erase() function
	it = lst1.begin();
	lst1.erase(it); // this removes the element where itertaor points
	
	// displaying remaining elements in the list
	cout << "remaining elements after doing all above operations " << endl;
	for(it =lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
	
	cout << endl;
	
	// checking list is empty or not
	lst1.empty() ? cout << "List is empty" << endl : cout << "List is not empty" << endl;
	
	// applying remove() method
	lst1.remove(11);
	
	// displaying remaining elements in the list
	cout << "remaining elements after removing 11 are" << endl;
	for(it =lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
	
	cout << endl;
	
	return 0;
}

Output

14 13 12 11 10
the first element of the list is 14
the last element of the list is 10
the first element after erasing current first elemnt is 13
the last element after erasing current last element is 11
remaining elements after doing all above operations
12 11
List is not empty
remaining elements after removing 11 are
12

turn around(): This switch every one of the components of the rundown.

sort(): Sorts the all components in the rundown in expanding request.

size(): This profits the number of components in the rundown.

Model program to appear above capacities:

#include<iostream>
#include<list>
#include<iterator>
 
using namespace std;
 
int main(){
	list <int> lst1;
	list <int> :: iterator it;
	
	for(int i=0; i<6; i++){
		if(i%2) lst1.push_back(i);
		else lst1.push_front(i);
	}
	
	cout << "actual elements of the list are" << endl;
	for(it = lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
	
	cout << endl;
	
	// using reverse function
	lst1.reverse();
	cout << "Elements in the list after applying reverse operation " << endl;
	for(it = lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
	
	cout << endl;
	
	// using sort function
	lst1.sort();
	cout << "Elements in the list after applying sort operation" << endl;
	for(it = lst1.begin(); it!= lst1.end(); it++)
	    cout << *it << " ";
 
	cout << endl;
	
	// finding size
	cout << "size of the lis is ";
	cout << lst1.size();
 
	return 0;
}

Output

Remark underneath on the off chance that you have questions or discovered data off base in above instructional exercise for C++ STL List.

Leave a Comment

error: Alert: Content is protected!!