c++ - std::find Object by Member -
scenario
i’ve run speedbump while using stl seems normal scenario, simplified here:
class person { string name; int age; }; vector<person> people; addpeople(people); string s("bob"); find(people.begin(), people.end(), s);
problem
unfortunatelyfind
wants compare entire class. question
there better or more appropriate way “stl way”? suggested questions weren’t helpful, managed find couple of related questions no direct solution.work-arounds/tests
there’s potential work-arounds:
forgo
find
altogether (cluttered, refactored):bool bbob = false; (uint = 0; < people.size(); i++) { if (people[i].name == s) bbob = true; break; }
provide conversion operator (implicit conversion doesn’t work; explicit can’t used in
find
):class person { string name; int age; operator string() {return name;} }; person b ("bob", 99); string s ("bob"); b == s; //doesn’t work string(b) == s; //works, no find()
define standalone equality operator (simple, effective, globally exposed):
bool operator==(person l, string r) { return l.name == r; }
define member equality operator (makes comparison order dependent; object must first):
class person { string name; int age; bool operator==(string s) {return name == s;} }; person b ("bob", 99); string s ("bob"); b==s; //works s==b; //doesn’t work, not problem find()
it looks #4 best candidate, none seem ideal or feel “stl”, , have problems.
is there better or more appropriate way “stl way”?
you can use std::find_if
(powered c++11 lambdas):
std::string name = "bob"; // ... std::find_if(std::begin(people), std::end(people), [&] (person const& p) { return p.name == name; }
notice, calling "stl way" inappropriate. c++ standard library, not stl ("standard template library"). stl served strong inspiration containers , algorithms library of c++ standard library, 2 things not same. see this q&a on stackoverflow further information.
edit:
since using compiler not support lambdas, can define own functor predicate:
struct person_has_name { person_has_name(std::string const& n) : name(n) { } bool operator () (person const& p) { return p.name == name; } private: std::string name; };
and use std::find_if
way:
std::string name = "bob"; // ... std::find_if(people.begin(), people.end(), person_has_name(name));
Comments
Post a Comment