Using std::ranges::for_each() with a Member Function

How do I use std::ranges::for_each() with a member function of a class?

To use std::ranges::for_each() with a member function of a class, you'll need to use a std::bind() or a lambda function to bind the member function to an instance of the class. Here's a simple example to illustrate this process:

First, let's define a class Player with a member function Log():

#include <iostream>
#include <vector>
#include <algorithm>

class Player {
public:
  Player(int id) : id{id} {}
  void Log() const {
    std::cout << "Player ID: " << id << "\n"; 
  }
private:
  int id;
};

Now, you can create a vector of Player objects and use std::ranges::for_each() to call the Log() member function on each Player object:

#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>

class Player {
 public:
  Player(int id) : id{id} {}
  void Log() const {
    std::cout << "Player ID: " << id << "\n";  
  }

 private:
  int id;
};

int main() {
  std::vector<Player> players{
    Player{1}, Player{2}, Player{3}};

  std::ranges::for_each(players, [](Player& p) {  
    p.Log();                                      
  });
}
Player ID: 1
Player ID: 2
Player ID: 3

In this example, we use a lambda function to call the Log() member function on each Player object. The lambda captures each Player object by reference and calls the Log() method.

Alternatively, you can use std::bind() if you prefer:

#include <algorithm>
#include <functional>
#include <iostream>
#include <ranges>
#include <vector>

class Player {
 public:
  Player(int id) : id{id} {}
  void Log() const {
    std::cout << "Player ID: " << id << "\n";  
  }

 private:
  int id;
};

int main() {
  std::vector<Player> players{
    Player{1}, Player{2}, Player{3}};

  std::ranges::for_each(players,
    std::bind(&Player::Log, std::placeholders::_1)
  );
}
Player ID: 1
Player ID: 2
Player ID: 3

In this second example, std::bind() is used to bind the Log() member function to each Player object.

Both approaches allow you to effectively use std::ranges::for_each() with member functions of a class.

8 Key Standard Library Algorithms

An introduction to 8 more useful algorithms from the standard library, and how we can use them alongside views, projections, and other techniques

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Comparing Only Part of Two Ranges
Can I use std::ranges::equal() to compare only a part of two ranges?
Using std::iota() with a Custom Step Size
How can I use std::ranges::iota() to generate a sequence with a custom step size?
Merging Three or More Sorted Ranges
How do I merge three or more sorted ranges using std::ranges::merge()?
Ensuring No Repeated Elements in std::ranges::sample()
Can I use std::ranges::sample() to ensure no repeated elements in the sample?
Removing Duplicates from a Range
What is the most efficient way to remove duplicates from a range using standard algorithms?
Using Subranges vs Iterator Pairs
What are the use cases for using std::ranges::subrange() versus standard iterator pairs?
std::ranges::set_union() vs std::ranges::merge()
What are the benefits of using std::ranges::set_union() over std::ranges::merge() in terms of performance and use cases?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant