I'm practicing C++, so this is not code that will go into production, but I'm very curious to know:
I have a vector of pointers to objects of type Player:
std::vector<Player*> _players;
Each player returns me his name as std::string when I call it's method get_name(), for example:
std::string player0_name = _players[0]->get_name();
I want to pass all player names to a function that expects them as reference to a vector of strings:
void all_player_names( std::vector< std::string >& );
Now I know it would be easy to do this via some temporary variable. I could first create a vector of strings, store all player names there, then pass it to the function all_player_names as reference.
But I'm looking for a way of doing that without having to create a temporary variable. It should be something similar like a list comprehension in Python. It has to iterate over the array of Player pointers, call the get_name() function on each of them, build a vector out of the returned strings and directly pass it to the function all_player_names. I'm assuming it should be possible with lambda functions and some algorithm from the STL, but I don't know which one.
So it should look more or less like that:
all_player_names(<function that i'm looking for>(
_players, [](Player* p) { return p->get_name();}
);
Is there such an algorithm in the STL?
var names = players.Select(p => p.Name):-)