C++ List and Standard Iterator


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:

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.

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

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:

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:

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

Push-Front() output

The complete listing and its output is given below:


Program Output

Program Output

Categories: C++

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: