Home » C++ STL

List functions in C++ STL (Standard Template Library)

C++ STL - List functions: Here, we are going to learn about the list functions in C++ Standard Template Library (C++ STL).
Submitted by IncludeHelp, on November 22, 2018

Following are the C++ STL list functions (public member functions) that can be used for various list operations, the functions are

Function Description Syntax (consider 'list' is the name of any integer list)
empty() Checks whether given list is empty or not. It returns 1 (true) if list is empty else it returns 0 (false). list.empty();
size() Returns size of the list list.size();
sort() Sorts the list in ascending order list.sort();
reverse() Reverses the list (elements of the list) list.reverse();
remove() Removes all occurrences of given elements from the list. list.remove(element);
remove_if() Remove set of the elements based on the test condition (if test condition is true for the element, element will be removed and it is applicable on all the occurrences of the element which satisfy the test condition). list.remove_if(test_condition);
front() Returns the first element of the list list.front();
back() Returns the last element of the list list.back();
push_front() Inserts the element at the front (beginning) to the list list.push_front(element);
push_back() Insert the element at the back (end) to the list list.push_back(element);
pop_front() Removes the element from the front (beginning) of the list list.pop_front();
pop_back() Removes the element from the back (end) of the list list.pop_back();
insert() Inserts the element at specified index/position list.insert(iterator_position, element);
list.insert(iterator_positon, number_of_elements, element);
begin() and end() Return the iterator pointing first and last element of the list list.begin(); and list.end();
rbegin() and rend() Return the iterator pointing first and last element of the list (in reverse order) i.e. first element will be considered as last and last will be consider as first list.rbegin(); and list.rend();
assign() Assigns the new set of elements or replaces the current with the new set of elements list.assign(n, element)
//will assign ‘element’, ‘n’ times to the list
merge() It merges two lists. list1.merge(list2);
unique() It removes consecutive elements from the list. list.unique();
erase() It removes the specified index or index from the given range (index1, index2), this function can be used by defining the positions using iterator. list.erase(iterator_position);
list.erase(iterator_position1, iterator_position2);

Ref: List in C++ STL


#include <iostream>
#include <list>
using namespace std;

//function to print the list 
void displayList(list<int> L){
	//declaring interator to the list
	list<int>::iterator l_iter;
	for(l_iter = L.begin(); l_iter !=L.end(); l_iter++)
		cout<<*l_iter<< " ";

//Main function
int main() {
	list<int>	ilist;

		cout<<"List is empty"<<endl;
		cout<<"List of not empty"<<endl;

	//push_front() and push_back()
	cout<<"List elemets aftre push_front(), push_back()"<<endl;

	list<int>::iterator it;
	it = ilist.begin();
	cout<<"List element after insert()"<<endl;

	cout<<"size of the list is: "<<ilist.size()<<endl;

	cout<<"List elements after sort()"<<endl;

	cout<<"List elements after reverse()"<<endl;

	//removeing 100 from the List
	cout<<"List elements after remove()"<<endl;

	//removeing elements divisible by 50
	ilist.remove_if([](int n) {return (n%50==0); });
	cout<<"List elements after remove_if()"<<endl;

	//front() and back()
	cout<<"First (front) element of the list: "<<ilist.front()<<endl;
	cout<<"Last (back) element of the list: "<<ilist.back()<<endl;

	//pop_front() and pop_back()
	cout<<"List element are pop_front() and pop_back()"<<endl;

	//begin() and end()
	//printing all elements with begin(() and end()
	//begin() and end() can be used with iterator
	list<int>::iterator l_itr;
	cout<<"List elements (using begin() and end()"<<endl;
	for (l_itr = ilist.begin(); l_itr != ilist.end(); l_itr++)
		cout<< *l_itr<< " ";

	//rbegin() and rend()
	//printing all elements witn rbegin() and rend()
	//rbegin() and rend() can be used with reverse_iterator
	list<int>::reverse_iterator l_ritr;
	cout<<"List elements (using rbegin() and rend()"<<endl;
	for (l_ritr = ilist.rbegin(); l_ritr != ilist.rend(); l_ritr++)
		cout<< *l_ritr<< " ";

	list<int> list2;
	cout<<"List2 elements after assign()"<<endl;

	cout<<"List elements after merge()"<<endl;

	cout<<"List  elements after unique()"<<endl;

	//it can be used with the iterator 
	it = ilist.begin();
	cout<<"List elements after erase()"<<endl;

	return 0;


List is empty
List elemets aftre push_front(), push_back()
22 11 30 20 10 40 50 
List element after insert()
100 200 300 22 11 30 20 10 40 50 
size of the list is: 10
List elements after sort()
10 11 20 22 30 40 50 100 200 300 
List elements after reverse()
300 200 100 50 40 30 22 20 11 10 
List elements after remove()
300 200 50 40 30 22 20 11 10 
List elements after remove_if()
40 30 22 20 11 10 
First (front) element of the list: 40
Last (back) element of the list: 10
List element are pop_front() and pop_back()
30 22 20 11 
List elements (using begin() and end()
30 22 20 11 
List elements (using rbegin() and rend()
11 20 22 30 
List2 elements after assign()
100 100 100 100 100 
List elements after merge()
30 22 20 11 100 100 100 100 100 
Listelements after unique()
30 22 20 11 100 
List elements after erase()
22 20 11 100 

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.