jeudi 29 octobre 2015

std::equal_range with lambda

Lets say I have a vector of strings and I want to find all strings, that start with 'a', so I can do this:

struct cmp {
    bool operator()( const std::string &s, char c ) const { return s.front() < c; }
    bool operator()( char c, const std::string &s ) const { return s.front() < c; }
};
std::vector<std::string> strings;
...
auto range = std::equal_range( strings.begin(), strings.end(), 'a', cmp{} );
...

Is it possible to implement comparison function with generic lambda instead of structure with 2 methods?

Aucun commentaire:

Enregistrer un commentaire