c++ - Search for value in multi map -
suppose have following:
class foo { public: foo(int x) { _x = x; } int _x; } int main() { multimap<string, foo> mm; foo first_foo(5); foo second_foo(10); mm.insert(pair<string, foo>("a", first_foo)); mm.insert(pair<string, foo>("a", second_foo)); foo third_foo(10); }
what's nice way of checking if third_foo
key "a"
in multimap
?
use multimap::equal_range
fetch range of iterators entries have key "a"
. use any_of
check if of values compare equal foo
want.
auto const& r = mm.equal_range("a"); bool found = std::any_of(r.first, r.second, [&third_foo](decltype(mm)::value_type const& p) { return p.second._x == third_foo._x; });
Comments
Post a Comment