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
List functions in C++ STL
Following are the C++ STL list functions (public member functions) that can be used for various list operations, the functions are
Function |
Description |
Syntax |
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); OR
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); OR
list.erase(iterator_position1, iterator_position2);
|
Ref: List in C++ STL
Example of List functions in C++ STL
Here is an example (program) of list functions 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 << " ";
cout << endl;
}
// Main function
int main() {
list<int> ilist;
// empty();
if (ilist.empty())
cout << "List is empty" << endl;
else
cout << "List of not empty" << endl;
// push_front() and push_back()
ilist.push_front(10);
ilist.push_front(20);
ilist.push_front(30);
ilist.push_front(11);
ilist.push_front(22);
ilist.push_back(40);
ilist.push_back(50);
cout << "List elemets aftre push_front(), push_back()" << endl;
displayList(ilist);
// insert()
list<int>::iterator it;
it = ilist.begin();
ilist.insert(it, 100);
ilist.insert(it, 200);
ilist.insert(it, 300);
cout << "List element after insert()" << endl;
displayList(ilist);
// size()
cout << "size of the list is: " << ilist.size() << endl;
// sort()
ilist.sort();
cout << "List elements after sort()" << endl;
displayList(ilist);
// reverse()
ilist.reverse();
cout << "List elements after reverse()" << endl;
displayList(ilist);
// remove()
// removeing 100 from the List
ilist.remove(100);
cout << "List elements after remove()" << endl;
displayList(ilist);
// remove_if()
// removeing elements divisible by 50
ilist.remove_if([](int n) { return (n % 50 == 0); });
cout << "List elements after remove_if()" << endl;
displayList(ilist);
// 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()
ilist.pop_front();
ilist.pop_back();
cout << "List element are pop_front() and pop_back()" << endl;
displayList(ilist);
// 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 << " ";
cout << endl;
// 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 << " ";
cout << endl;
// assign()
list<int> list2;
list2.assign(5, 100);
cout << "List2 elements after assign()" << endl;
displayList(list2);
// merge()
ilist.merge(list2);
cout << "List elements after merge()" << endl;
displayList(ilist);
// unique()
ilist.unique();
cout << "List elements after unique()" << endl;
displayList(ilist);
// erase()
// it can be used with the iterator
it = ilist.begin();
ilist.erase(it);
cout << "List elements after erase()" << endl;
displayList(ilist);
return 0;
}
Output
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