CPP Tutorial

CPP examples programs

CPP List

The function of List is more or less same as CPP vector, however unlike the CPP vectors, CPP list is a contiguous container which supports a bidirectional insertion and deletion operations. In CPP list, elements are accessed sequentially Which results in slow traversal.

Syntax 1:

list <data_type>  list_name{value1, value2, …….};

Syntax 2:

list <data_type>  list_name = {value1, value2, …….};

CPP Deque Functions

FUNCTION USES
assign() To assign a new element to the list container.
back() To access the last element.
empty() To determine whether the list is empty or not.
emplace_back() To insert a new element at the end.
emplace_front() To insert a new element at the beginning.
emplace() To insert a new element at a specified position.
front() To access the first element.
insert() To insert a new element just before the specified position.
merge() To merge the two sorted list.
max_size() To determine the maximum size of the list.
push_back() To add a new element at the end of the list.
push_front() To add a new element at the start of the list.
pop_back() To remove the last element from the list.
pop_front() To remove the first element from the list.
resize() To modify the size of the list.
 reverse() To reverse the order of the list.
size() To determine the number of elements in the list.
sort() To sort the list in an increasing order.
splice() To insert a new list into the invoking list.
swap() To exchange the the contents of two lists of same type.
unique() To remove all the duplicate elements from the list.

 

Example:

#include<iostream.h>  
#include<list.h>  
using namespace std;  
int main()  
{  
list<string> ls{"Hello! ", "CPP ", "Programmers. ", "Welcome !!"};  
for (list <string> ::iterator i=ls.begin(); i!=ls.end(); ++i)  
cout << *i;  
return 0;   
}

Output

Hello! CPP Programmers. Welcome !!