C++

Iterate Through A List In C++

The most common data structure is a list. A list is a collection of records linked together so that they can be simply iterated and maintained. An iterator is an attribute (similar to a pointer) that points to a component of the list. Iterators can be used to traverse the data of the list. They will be seen as a pointer indicating a certain point, and then we could use them to retrieve data at that specific region.

Iterators are important for integrating algorithms into lists and modifying data stored within the lists. A pointer has been the most common type of iterator. A pointer can relate to attributes in an array and then use the increment operator (++) to traverse over them. However, not all iterators have the same adaptability as pointers.

The C++ list would be iterated in both modes (that is, forward and backward). We will construct a C++ list and iterate through its components in this article. And this article will go over all of the techniques in C++ that will be used to iterate through a list.

Use An Iterator To Iterate Through A List

In this procedure, an iterator ‘itr’ is constructed and initialized utilizing begin() method, which would indicate the first component. It will iterate till it approaches the end of the list, with ‘itr’ indicating the next component in the list. In this method, two functions would be used:

  • begin() provides an iterator to the initial component of the list.
  • end() provides an iterator to the qualitative component that comes after the last component of the list.
#include<bits/stdc++.h>

using namespace std;
void display(set a)
{
set::iterator itr;
for (itr = a.begin();
    itr != a.end(); itr++)
{
    cout<< *itr<< " ";
}
}

int main()
{
set a;
a.insert(350);
a.insert(550);
a.insert(750);
a.insert(450);
a.insert(650);
display(a);
return 0;
}

Initially, we apply the void display() function to show the components of the set. The variable ‘a’ is being specified for these elements. For representing the elements, we have been used for loop. Within for loop we apply begin() and end() functions. The begin() method returns an iterator having the value indicating the first component. It differs from the iterator’s front() method in which the front() function provides a pointer, whereas begin() provides the iterator directly. The end() function reverts an iterator that leads to the last component of the list. We do increment in the value of the iterator.

We employ the ‘cout’ statement for the pointer of the iterator. First, we insert the numbers in random order. The insert() method is being used to insert these numbers. If we want to display all these numbers on the list, so display() method is utilized. To end the code, we enter the ‘return 0’ command.

Use A Range-Based For Loop To Iterate Through A List

A range-based for loop is being utilized to iterate over most of the components in a list in a forward manner in this methodology.

#include<bits/stdc++.h>

using namespace std;
void display(set c)
{
for (auto itr : c)
{
    cout<<itr<< " ";
}
}
int main()
{

set c;
c.insert(7);
c.insert(4);
c.insert(1);
c.insert(8);
c.insert(3);
display(c);
return 0;
}

First of all, we introduce the library <bits/stdc++.h>. In the next line, we will utilize the standard namespace. We have been using the void display() method to show the entities of the list. We set the variable ‘c’ to store the entities. Now for displaying these entities, the list ‘for’ loop is applied.

The first parameter represents the declaration of range. A definition or a pointer to a specified variable whose kind is the same as that of the item in the order specified by expression of range. For autonomous type induction, the auto qualifier is frequently used. The second parameter of the ‘auto’ function shows the range of the expression. It indicates an appropriate order. We add the loop statement by using ’cout’.

Here we initialize the object of the specified list within the body of the main() function. We randomly add some numbers by employing the c.insert() function for all numbers. The display() function is being used to show these random numbers. The defined set is passed as a parameter to this function. We use the ‘return 0’ command for the termination of code.

Use Reverse Iterator To Iterate Through A List Backward

In this technique, a reverse iterator ‘itr’ is constructed and initialized with the rbegin() method to indicate the last component in a list, but after every iteration, ‘itr’ relates to the next component in a list in a reverse manner, and iterates till it attains the beginning of the list.

#include<bits/stdc++.h>

using namespace std;

void display(set x)
{
set::reverse_iteratoritr;
for (itr = x.rbegin();
    itr != x.rend(); itr++)
{
    cout<< *itr<< " ";
}
}
int main()
{

set x;
\x.insert(600);
x.insert(400);
x.insert(800);
x.insert(700);
x.insert(200);
display(x);
return 0;
}

At the beginning of the program, we integrate the header file <bits/stdc++.h>. We also make use of the standard namespace. We have used the void display() method to exhibit the set’s contents. To store these components, we have stated the variable ‘x’. We’ve used a ‘for’ loop to indicate the elements. The rbegin() and rend() procedures are applied within the for loop. The rbegin() is an in-built method that provides a reverse iterator pointed to the last component of the list.

The rend() is also the in-built method that generates a reverse iterator leading to the hypothetical component preceding the first component in the list. An iterator is generated, and it would start and proceed until it attains the end of the list by incrementing in every loop.

After that, we used the ‘cout’ command to get the iterator’s pointer. The main() function is then invoked. In this case, the numbers are entered in a randomized order. The insert() function is used to incorporate integers into a list. Hence we’ll apply the display() method to show all of the numbers in the list. In the end, we enter the command ‘return 0’ to terminate the program.

Conclusion

In this article, we have discussed several methods of iterating through the list in C++. We will be iterating through a list with the help of an iterator, range-based for loop, and reverse iterator. All these methodologies have been described in some programs.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content