C++

List iterator C++

A list is a data structure that contains the items in it in the form of a sequence. Just like arrays, it also contains the members of the same data type at a time. In this guide, we will learn about the iterators in the list in the C++ programming language.

Iterators

An iterator acts like a pointer that indicates the items inside the list. Iterators are used mainly to move through the data inside a list. Iterators are also used to indicate the memory address of the containers. In this article, some operations of iterators are explained.

Begin()

This begin() function has an iterator that is returned with the value showing the first element. It is quite different from the front() function of the iterator because the front function returns a reference, but the begin() returns the iterator itself.

End()

It returns an iterator that points towards the last element of the list.

Advance()

This is an important operation as it is used to increment the iterator’s position to the specified number mentioned in the parameter.

Next()

This function returns the new iterator after it has advanced its position, which is mentioned in the argument.

Prev()

This is the function that is used to bring the new iterator that shows the value that has been decremented to the described number in the list.

Inserter()

This is the function that is used to insert the new elements in any position in the list. It has two arguments. One is the container name, and the other one is the pointer that shows the position where the new item or items are to be inserted.

Example 1

Begin (), end ():

This example contains the source code to demonstrate the working of these two functions. As we are applying these functions on the list, the library for the list is used here in the first step.

#include <list>

Then inside the main program, we declare a list with 5 integers.

List <int> mylist {1,2,3,4,5};

Now we want to display all the content of the list. So a FOR loop is used. As we know, a for loop requires a starting and ending point in the loop initiative to start the loop in the list “mylist”. begin() function is used here. And at the end, “mylist. end()” is used. ‘mylist’ is the object that is used to access the list.

An iterator is created that will start and move till the end by incrementing in each loop. To execute the source code, use a G++ compiler for compilation and then execution of the code. Go to the Linux terminal and then use the below-cited commands.

$ g++ -o file file.c

$ ./file

The resultant value will contain all the numbers that we have entered into the list through the main program.

Example 2

Advance ():

As described above, this function is used to increment the iterator to a specific number passed as its argument. Now consider the following example in which we have applied an iterator class with the list class so that all the functions of iterators can be performed easily.

After the declaration of a list, the iterator is declared to the list.

List <int> iterator ::ptr = mylist.begin();

“Ptr” is an object of the iterator. This iterator is assigned the begin() function. Now we need to jump towards the particular point, we will increment the iterator position up to 2, and it is done by using the advance () function.

Advance (ptr,2);

This function takes the object of the iterator and the number to show the position where we want to shift the iterator. After that, the position of the iterator at that point is displayed. By default, it was at the first position; by using advance, now it will be at 3.

Save the source code in the file and then run it to see the executed values.

Example 3: Next(), prev()

Both iterators are returned with the value when the iterator is incremented and once the iterator is decremented. The positions are mentioned in the argument, as they are described in advance () function parameter. After using the libraries inside the main program, the list is declared first in the example. After that, iterators are created and then declared to the list. Two separate iterators are created with different iterator pointer objects for both functions.

First, we will use the next iterator that will return the new iterator that is pointing towards 4.

Auto it = next(ptr, 3);

The iterator is auto-generated and will call the next function with the pointer and the number we want the iterator to be incremented. So just like the advance() function, the iterator will increment towards the given position. Now the next step is to use the prev() function. The prev() will also contain the object pointer created above and the number to go back. This function will return a new iterator that will point towards 3.

Auto it1 = prev(ftr, 3);

In the end, we will display the position of new iterators in both cases. Both values are displayed by using the pointers, as they store the positions in the list. Compile the code, and then you will see that both the functions of iterators prev() and next() display the values in the list through the compiler.

Example 4

Inserter()

As we have explained above, this function inserts the new value at any position in the list. So now, in the example, first declare a list with 3 numbers of integers.

In inserter(), we can insert a single number and a list of numbers. So in this example, we will insert a list of three numbers inside the list we have created above. Create an iterator and then declare it to the list we have created. As we have to insert the new list in a specific place, we need to increment the iterator at that position. And this is done by using the Advance() iterator.

Advance( ptr, 2);

It will increment the iterator to the 2nd position; it means after 2, the new list will be inserted. So with the help of objects, elements of one list will copy into the second list. We will use a copy function here.

Copy( ar1.begin(), ar1. end(), inserter(ar, ptr));

The copy function will use the begin() and end(), to start copying the elements from the start of the second list and keep copying till the end of the list. The inserter function takes the object of the first list and the iterator object to point out the position where the list has to be inserted.

To display all the elements, we will use a FOR loop.

The resultant values that are obtained from the execution of the above code show that the first list contains the numbers of both lists.

Conclusion

‘List iterator C’ is the article that contains the information regarding the types of articles that we applied to the list. All these operations are explained with the basic description. We have also quoted some examples for each function of the iterator. These examples are implemented in the Linux operating system using the text editor and the Linux terminal.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.