Updated April 13, 2023
Introduction to C++ List
The list is a type of sequence container data structure. Generally, the list denotes a double linked list thereby allowing access to the data in both the directions. The insertion and deletion can take place anywhere within the container and both these operations take constant time. The link is maintained internally by pointing to the previous element and to the next element. However, lists are not the performed data structure for random access. Lists can alter its size based on the number of elements from both ends during run time. This article will cover in detail about lists and their associated functions.
Syntax:
template < class T, class Alloc = allocator<T>> class list;
- T denotes the data type; it can be even a user defined one.
- Alloc- It denotes the memory allocation model to be used.
Example:
List<int>integer_list
Member Types in C++ List
The following are the member types that can be used as return types or even as parameters.
- Value_type
- Allocator_type
- Reference
- Const_reference
- Pointer
- Const_pointer
- Iterator
- Const_iterator
- Reverse_iterator
Functions of the List in C++
The functions associated with a list can be categorized as follows:
1. Member Functions
- Constructor(): The default constructor for the list.
- Destructor(): The default destructor for the list.
2. Iterators
- begin(): This is used to bring the iterator back to the beginning position.
- end(): This returns the iterator to the end.
- rbegin(): This returns the iterator to the end.
- rend(): This returns the iterator to the beginning.
- cbegin(): This returns the constant iterator to the beginning position.
- cend(): This points the constant iterator to the end.
- crbegin(): This is the reverse of cbegin.
- crend: This is the reverse of cend.
3. Capacity
- empty(): This is used to determine if the list is empty or not.
- size(): This function returns the size of the list.
- max_size(): This function determines the maximum size of the list.
4. Access
- front(): This function is used to access the first element.
- back(): This function is used to access the last element.
5. Modifiers
- push_front(): This function is used to insert an element at the beginning of the list.
- pop_front(): This function deletes the first element.
- push_back(): This function adds the element in the last position.
- pop_back(): This function deletes the element in the last position.
- insert(): This is used to insert elements from the list.
- erase(): This is used to erase elements from the list.
- swap(): This function swaps the elements of the two lists.
- resize(): This is used to resize the list.
- clear(): This empties the list content.
6. Operations
- splice(): This function is used to transfer elements from one list to another.
- remove(): This is used to remove the element with the specific value.
- removeif(): This function removes the element that satisfies the specified condition.
- merge(): This is used to merge two lists.
- sort(): This is used to sort the elements in the specified order.
- reverse(): This reverses the elements in the list.
Example to Implement C++ List
Below are the examples of C++ List:
Code:
#include <iostream>
#include<list>
using namespace std;
intmain()
{ cout<<"\nWelcome to list demo in C++";
list<int>testli={10,20,30,40};
list<int>::iterator titr=testli.begin();
list<int> testli1={22,34,45,55,65,75,85};
list<int>::iterator titr1=testli.begin();
//insert function demo
testli.insert(titr,50);
cout<<"\n inserting new element using insert fucntion";
cout<<"\n now the list values are :\t";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//push back function
testli.push_back(70);
testli.push_back(80);
cout<<"\n now the list values are(new values are pushed at the last) :\t";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//push front function
testli.push_front(66);
testli.push_front(13);
cout<<"\n now the list values are(new values are pushed at the front) :\t";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//pop back function
testli.pop_back();
testli.pop_back();
cout<<"\n now the list values are(the last two elements are deleted):\t";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//pop front function
testli.pop_front();
testli.pop_front();
cout<<"\n now the list values are(the first two elements are deleted):\t";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//size function
cout<<"\nlist size is"<<testli.size();
cout<<"\nmax list size is"<<testli.max_size();
//front and back function
cout<<"\nfronf element in list is"<<testli.front();
cout<<"\last element in list is"<<testli.back();
//swap example
cout<<"\ncontents of first list before swaping";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
cout<<"\ncontents of second list before swaping";
for(titr1=testli1.begin();titr1!=testli1.end();++titr1)
cout<<" "<<*titr1;
testli.swap(testli1);
cout<<"\nafterswaping";
cout<<"\nfirst list elements";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
cout<<"\nsecond list elements";
for(titr1=testli1.begin();titr1!=testli1.end();++titr1)
cout<<" "<<*titr1;
//reverse function example
cout<<"\nactual list";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
testli.reverse();
cout<<"\nafter reverse";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//sort function example
cout<<"\nbefore sorting, the elements are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
testli.sort();
cout<<"\nsorted in ascending order";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//merging two lists
cout<<"\nbeforemergiing, the elements are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
testli.merge(testli1);
cout<<"\naftermerging,the elements are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//printing only unique values
testli.push_front(10);
testli.push_front(20);
testli.push_front(30);
testli.push_front(30);
cout<<"\nthe elements in the list are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
testli.unique();
cout<<"\nthe unique elements in the list are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//resize example
cout<<"\ncurrent list size is"<<testli.size();
cout<<"\nreducing to only 5 elements";
testli.resize(5);
cout<<"\nthe elements in the list are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//assign example
list<int> testli2;
list<int>::iterator titr2=testli2.begin();
cout<<"\nthe elements in the list are";
for(titr2=testli2.begin();titr2!=testli2.end();++titr2)
cout<<" "<<*titr2;
testli2.assign(testli.begin(),testli.end());
cout<<"\nnow the list contents are";
for(titr2=testli2.begin();titr2!=testli2.end();++titr2)
cout<<" "<<*titr2;
//emplace function
cout<<"\nthe elements in the list are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
testli.emplace(testli.begin(),100);
cout<<"\nsee where the new element is placed";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//empalce back and front
cout<<"\nthe elements in the list are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
testli.emplace_back(11);
testli.emplace_back(21);
testli.emplace_back(31);
testli.emplace_back(41);
cout<<"\nthe elements in the list are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
cout<<"\nthe elements in the list are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
testli.emplace_front(111);
testli.emplace_front(211);
testli.emplace_front(311);
testli.emplace_front(411);
cout<<"\nthe elements in the list are";
for(titr=testli.begin();titr!=testli.end();++titr)
cout<<" "<<*titr;
//check if the list is empty
if(testli.empty())
cout<<"\nlist is empty";
else
cout<<"\nlist is not empty";
//clearing the contents of the list
cout<<"\nsize of the list before clearing is:"<<testli.size();
testli.clear();
cout<<"\nsize of the list now is"<<testli.size();
if(testli.empty())
cout<<"\nlist is empty";
else
cout<<"\nlist is not empty";
return 0;
}
Output:
Conclusion
Thus, the article explained in detail about the list data structure in C++. It also explained in detail about the functions that are associated with a list, the usage of the function, and its syntax. The article also demonstrated an example for each of the functions and the corresponding output. To learn more in detail it is advisable to write sample programs and practice them.
Recommended Article
This is a guide to C++ List. Here we discuss the Introduction to C++ List Functions and its Example along with code implementation and Output. you can also go through our suggested articles to learn more –