Iterate through a list c++: In the previous article, we have discussed about Difference between Vector and List in C++. Let us learn how to Iterate over a List of Objects in C++ Program.
4 Different ways to Iterate Over a List of Objects
C++ list iterator: In this article we will learn 4 different techniques for iterating through std::list of objects.
- Iterate over list using Iterators
- Iterate over list using std::for_each and Lambda Function
- Iterate over list in Reverse Order using reverse_iterator
- Iterate over list using c++11 Range Based For Loop
Suppose, we have a struct Student which contains roll and name.
struct Student
{
int roll;
std::string name;
Student(int studRoll, std::string studName) :
roll(studRoll), name(studName)
{
}
bool operator <(const Student & studObj) const
{
return roll < studObj.roll;
}
};
Lets create a list of Student objects,
std::list<Student> list_Students = { Student(15, "Aditya"),
Student(6, "Sujeet"),
Student(41, "Arhan"),
Student(28, "Dipti"),
Student(3, "Tamanna"),};
Now let’s try implementing different ways to iterate over list of Student.
Method 1- Iterate over list using Iterators :
C++ list iterator: It involves several steps.
- First we will create a std::list iterator.
- Initially iterator points to the first element.
- Keep incrementing iterator till the last element of the list.
- During each pass, access element through iterator
To create Iterator of std::list
std::list<Student>::iterator iter;
To make iterator point to beginning and iterate till the end of the list
for (iter = list_Student.begin(); iter != list_Student.end(); iter++)
{
// Access the object elements
int roll = iter->roll;
std::string name = iter->name;
//Print the contents available
std::cout << roll << " :: " << name << std::endl;
}
Let’s see the below program to understand it clearly.
#include <list>
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
struct Student
{
int roll;
std::string name;
Student(int studRoll, std::string studName) :
roll(studRoll), name(studName)
{
}
bool operator <(const Student & studObj) const
{
return roll < studObj.roll;
}
};
int main(){
std::list<Student> list_Students = { Student(15, "Aditya"),
Student(6, "Sujeet"),
Student(41, "Arhan"),
Student(28, "Dipti"),
Student(3, "Tamanna"),};
std::cout<<"List of Student : "<<std::endl;
//Iterator of std::list
std::list<Student>::iterator iter;
// Make iterator point to beginning and iterate till the end of the list
for (iter = list_Students.begin(); iter != list_Students.end(); iter++)
{
// Access the object elements
int roll = iter->roll;
std::string name = iter->name;
//Print the contents available
std::cout << roll << " :: " << name << std::endl;
}
}
Output : List of Student : 15 :: Aditya 6 :: Sujeet 41 :: Arhan 28 :: Dipti 3 :: Tamanna
Method 2- Iterate over list using std::for_each and Lambda Function :
Similarly an STL algorithm i.e. std::for_each accepts a range and a function,
template <class InputIter, class Function> Function for_each (InputIter begin, InputIterator last, Function func);
It iterates over the range and pass element to function func, and we will just have to pass begin() and end() of list as range arguments and lambda function as third argument e.g.
std::for_each(list_Students.begin(), list_Students.end(),
[](const Student & stud)
{
//Access and print the contents
std::cout<<stud.roll<< " :: "<<stud.name<<std::endl;
});
Let’s see the below program to understand it clearly.
#include <list>
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
struct Student
{
int roll;
std::string name;
Student(int studRoll, std::string studName) :
roll(studRoll), name(studName)
{
}
bool operator <(const Student & studObj) const
{
return roll < studObj.roll;
}
};
int main(){
std::list<Student> list_Students = { Student(15, "Aditya"),
Student(6, "Sujeet"),
Student(41, "Arhan"),
Student(28, "Dipti"),
Student(3, "Tamanna"),};
std::cout<<"List of Student : "<<std::endl;
std::for_each(list_Students.begin(), list_Students.end(),
[](const Student & stud)
{
//Access and print the contents
std::cout<<stud.roll<< " :: "<<stud.name<<std::endl;
});
}
Output : List of Student : 15 :: Aditya 6 :: Sujeet 41 :: Arhan 28 :: Dipti 3 :: Tamanna
Method 3- Iterate over list in Reverse Order using reverse_iterator :
We can even print the list in reverse order.
- list::rbegin() – It returns a revese_iterator which points to the end of std::list
- list::rend() – It returns a revese_iterator which points to the beginning of std::list
Let’s see the below program to understand it clearly.
#include <list>
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
struct Student
{
int roll;
std::string name;
Student(int studRoll, std::string studName) :
roll(studRoll), name(studName)
{
}
bool operator <(const Student & studObj) const
{
return roll < studObj.roll;
}
};
int main(){
std::list<Student> list_Students = { Student(15, "Aditya"),
Student(6, "Sujeet"),
Student(41, "Arhan"),
Student(28, "Dipti"),
Student(3, "Tamanna"),};
std::cout<<"List of Student : "<<std::endl;
for (const Student & stud : list_Students)
{
std::cout << stud.roll << " :: " << stud.name << std::endl;
}
std::cout<<"\nList of Student in reverse order : "<<std::endl;
// reverse iterator for std::list
std::list<Student>::reverse_iterator revIter;
// MAke iterator point to beginnig and increment till it reahes end of the list
for (revIter = list_Students.rbegin(); revIter != list_Students.rend(); revIter++)
{
// Access the Student objects through iterator
int roll = revIter->roll;
std::string name = revIter->name;
//Print the contents in reverse order
std::cout << roll << " :: " << name << std::endl;
}
}
Output : List of Student : 15 :: Aditya 6 :: Sujeet 41 :: Arhan 28 :: Dipti 3 :: Tamanna List of Student in reverse order : 3 :: Tamanna 28 :: Dipti 41 :: Arhan 6 :: Sujeet 15 :: Aditya
Method 4- Iterate over list using c++11 Range Based For Loop :
We can also iterate over a list taking a for loop.
Let’s see the below program to understand it clearly.
#include <list>
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
struct Student
{
int roll;
std::string name;
Student(int studRoll, std::string studName) :
roll(studRoll), name(studName)
{
}
bool operator <(const Student & studObj) const
{
return roll < studObj.roll;
}
};
int main(){
std::list<Student> list_Students = { Student(15, "Aditya"),
Student(6, "Sujeet"),
Student(41, "Arhan"),
Student(28, "Dipti"),
Student(3, "Tamanna"),};
std::cout<<"List of Student : "<<std::endl;
for (const Student & stud : list_Students)
{
std::cout << stud.roll << " :: " << stud.name << std::endl;
}
}
Output : List of Student : 15 :: Aditya 6 :: Sujeet 41 :: Arhan 28 :: Dipti 3 :: Tamanna