Something that I have to do quite often is finding a member in a collection of elements which has an element with a given value. For example given:
class Person
{
string getName() const {return mName;}
private:
string mName;
};
std::vector<Person> people;
I want to find the Person whose name is "Alice". One way to do this would be (using boost range adaptors):
string toFind = "Alice";
auto iterator = find(people | transformed([](Person const & p){p.getName()}) , toFind );
this is a lot of boilerplate for such a simple operation. Shouldn't it be possible to do something like:
string toFind = "Alice";
auto iterator = find(people | transformed(&Person::getName) , toFind );
(doesnt compile because &Person::getName is not a unary function)
Is there an easy way to get a unary function for a member?