C++ : Different Ways to iterate over a List of objects

In this article we will discuss different ways to iterate through std::list of objects.

Suppose we have a struct Player that contains id and name i.e.

struct Player
{
	int id;
	std::string name;

	Player(int playerId, std::string playerName) :
			id(playerId), name(playerName)
	{
	}
};

Lets create a list Player objects i.e.

std::list<Player> listofPlayers = { Player(22, "Sid"),
									Player(3, "Laura"),
									Player(43, "Riti"),
									Player(30,"Angel"),
									Player(2, "Laura")
									};

Now we will iterate through list of Player objects using different techniques i.e.

Iterating through list using Iterators

Steps:

  • Create an iterator of std::list.
  • Point to the first element
  • Keep on increment it, till it reaches the end of list.
  • During iteration access, the element through iterator
//Create an iterator of std::list
std::list<Player>::iterator it;

// Make iterate point to begining and incerement it one by one till it reaches the end of list.
for (it = listofPlayers.begin(); it != listofPlayers.end(); it++)
{
	// Access the object through iterator
	int id = it->id;
	std::string name = it->name;
	
	//Print the contents
	std::cout << id << " :: " << name << std::endl;

}

Iterating through list using std::for_each and Lambda Function

std::for_each is an STL algorithm that accepts a range and a function i.e.

template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function fn);

It iterates over the given range and passes each element in range to the passed function fn.

We will pass begin() and end() of list as range arguments and lambda function as third argumen i.e.

std::for_each(listofPlayers.begin(), listofPlayers.end(),
		[](const Player & player)
		{
			//Print the contents
			std::cout<<player.id<< " :: "<<player.name<<std::endl;
		});

Iterating through list using c++11 Range Based For Loop

for (const Player & player : listofPlayers)
{
	std::cout << player.id << " :: " << player.name << std::endl;
}

Iterating through list in Reverse Order using reverse_iterator

  • list::rbegin() returns a reverse_iterator which points to the end of list
  • list::rend() returns a reverse_iterator which points to the beginning of list

reverse_iterator will iterate in backwards only.

	//Create a reverse iterator of std::list
	std::list<Player>::reverse_iterator revIt;

	// Make iterate point to begining and incerement it one by one till it reaches the end of list.
	for (revIt = listofPlayers.rbegin(); revIt != listofPlayers.rend(); revIt++)
	{
		// Access the object through iterator
		int id = revIt->id;
		std::string name = revIt->name;

		//Print the contents
		std::cout << id << " :: " << name << std::endl;

	}

Complete executable example is as follows,

#include <list>
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

struct Player
{
	int id;
	std::string name;

	Player(int playerId, std::string playerName) :
			id(playerId), name(playerName)
	{
	}
};

int main()
{
	std::list<Player> listofPlayers =
	{ Player(22, "Sid"), Player(3, "Laura"), Player(43, "Riti"), Player(30,
			"Angel"), Player(2, "Laura") };

	std::cout << "*******Iterate std::list using Iterators*******" << std::endl;

//Create an iterator of std::list
	std::list<Player>::iterator it;

// Make iterate point to begining and incerement it one by one till it reaches the end of list.
	for (it = listofPlayers.begin(); it != listofPlayers.end(); it++)
	{
		// Access the object through iterator
		int id = it->id;
		std::string name = it->name;

		//Print the contents
		std::cout << id << " :: " << name << std::endl;

	}

	std::cout
			<< "*******Iterate std::list using for_each and c++11's Lambda function *********"
			<< std::endl;

	std::for_each(listofPlayers.begin(), listofPlayers.end(),
			[](const Player & player)
			{
				//Print the contents
				std::cout<<player.id<< " :: "<<player.name<<std::endl;
			});

	std::cout
			<< "*******Iterate std::list using c++11 Range Based For Loop *********"
			<< std::endl;

	for (const Player & player : listofPlayers)
	{
		std::cout << player.id << " :: " << player.name << std::endl;
	}

	std::cout
			<< "*******Iterate std::list in backwords using Iterators *********"
			<< std::endl;

	//Create a reverse iterator of std::list
	std::list<Player>::reverse_iterator revIt;

	// Make iterate point to begining and incerement it one by one till it reaches the end of list.
	for (revIt = listofPlayers.rbegin(); revIt != listofPlayers.rend(); revIt++)
	{
		// Access the object through iterator
		int id = revIt->id;
		std::string name = revIt->name;

		//Print the contents
		std::cout << id << " :: " << name << std::endl;

	}

	return 0;

}

Output

22 :: Sid
3 :: Laura
43 :: Riti
30 :: Angel
2 :: Laura
*******Iterate std::list using for_each and c++11's Lambda function *********
22 :: Sid
3 :: Laura
43 :: Riti
30 :: Angel
2 :: Laura
*******Iterate std::list using c++11 Range Based For Loop *********
22 :: Sid
3 :: Laura
43 :: Riti
30 :: Angel
2 :: Laura
*******Iterate std::list in backwords using Iterators *********
2 :: Laura
30 :: Angel
43 :: Riti
3 :: Laura
22 :: Sid

To compile above code on linux use following command,

g++ –std=c++11 example.cpp

1 thought on “C++ : Different Ways to iterate over a List of objects”

  1. Pingback: C++ std::list Tutorial, Example and Usage Details – thisPointer.com

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top