1. About C++ List
In CPP, List is the most used data structure. A list is chunks of similar memories (type) linking in such a way that it can be easily iterated and managed. We can iterate the C++ list in both direction (i.e.) we can iterate it in forward and reverse directions. C++ standard list implements it as “Doubly Linked List”, but we no need to worry much about it. In this article, we will see creating C++ list and iterating through the elements of it.
2. Declaring and Adding Elements to C++ List
We can declare the
std::list
as follow:
1 |
list<int> theList; |
Before placing the declaration, include the header
<list>
. In the above declaration, we specified that the variable
theList
will have the chain of linked memories that can store integers. Once after declaring the C++ list, we can add integer numbers to it using the
push_back()
function. The below code adds three integers to the List.
1 2 3 4 5 |
//Sample 1.0 Declare and Add elements to list list<int> theList; theList.push_back(2); theList.push_back(4); theList.push_back(6); |
The push_back() function always adds the element at the end of the std::list. The below picture shows the list at this moment:

Adding List elements through push_back
3. Traversing the List Using List::Iterator
An Iterator is a type and as the name suggest, it iterates the C++ list in both forward and reverse direction. Using the iterator, one can change the list elements. We can declare a list iterator as shown below:
1 |
list<int>::iterator list_itr; |
Here,
list_itr
is an iterator that will iterate the list of integers. Now have a look at the below code:
Once we declare the iterator, the
begin()
method of the C++ list is used to get the iterator which has position set to the start point of the list
theList
(Marked as 1). Now through this iterator
list_itr
, we can get the first list item. The for loop ends when the iterator position reaches the end of the list. To do that, here in our example, the
end()
method of the C++ list is used to get the iterator pointing to the end of the list and this position compared with the other iterator (One we are using to traverse)
list_itr
(Marked as 2). During each for loop iteration, we increment the
list_itr
and this will advance the iterator to the next item. Note that to get the value from its current position, we are using pointer dereferencing
*list_itr
.
4. The push_front Function
The
push_front()
function adds the element towards the beginning of the C++ List. When we keep on adding, all the new elements stays in the beginning of the list. This is just the reverse of what
push_back()
is doing. Now, have a look at the below example:
1 2 3 4 5 6 7 |
//Sample 4.0 Push_Front theList.push_front(1); theList.push_front(0); printf("\nThe list elements after push front are:\n"); list_itr = theList.begin(); for (list_itr; list_itr != theList.end(); list_itr++) printf("[%d] ", *list_itr); |
The output of the above code snippet is shown below. Note that we added the ‘element 1’ first and then added the integer ‘element 0’. In the list, 0 stays first and 1 stays next. This means, the
push-front()
adds the element at the beginning and all the existing elements shifts one index from its current position.

Push-Front() output
The complete listing and its output is given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include "stdafx.h" #include <stdio.h> #include <conio.h> #include <list> using namespace std; //Example 01 void main() { //Sample 1.0 Declare and Add elements to list list<int> theList; theList.push_back(2); theList.push_back(4); theList.push_back(6); //Sample 3.0 Iterate through the list list<int>::iterator list_itr; printf("The list elements are:\n"); list_itr = theList.begin(); for (list_itr; list_itr != theList.end(); list_itr++) printf("[%d] ", *list_itr); //Sample 4.0 Push_Front theList.push_front(1); theList.push_front(0); printf("\nThe list elements after push front are:\n"); list_itr = theList.begin(); for (list_itr; list_itr != theList.end(); list_itr++) printf("[%d] ", *list_itr); _getch(); } |
Output

Program Output
- How Do I Write A C++ VARIADIC Function using va_start, va_arg, va_end Macros?
- C++ std::list Clear vs Erase vs Empty Explained
Categories: C++
Tags: C++ List, list iterator, push_back, push_front