In this article we will discuss how to iterate over a container of user defined objects and call a member function on each of the iterating element.
Suppose you have a vector of Employee class objects and you want to call a member function on each of the element in vector.

Let’s see how to do this using std::for_each,

First Create an Employee Class,

Then Create a std::vector of Employee Pointers and initialize that,

Now Iterate through all the elements in vector and call displayEmployeeInfo member function for each of the element,
Also, In the end delete all the elements using std::for_each again.

Python Resources

C++11 / C++14 Resources

Design Patterns Resources

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,