c++ - find an item in a list of pointers -
i trying understand how find item in list of pointers in c++, using std::find
if had example:
std::list<string> words; std::string word_to_be_found;
i search this:
std::list<string>::iterator matching_iter = std::find(words,begin(), words.end(), word_to_be_found)
but if have lsit of pointers?
std::list<string *> words;
the above syntax not work anymore. can similar way?
thanks!
you can pass predicate std::find_if
function:
bool pointee_is_equal(const std::string& s, const std::string* p) { return s == *p; } // ... std::list<string>::iterator matching_iter = std::find_if(words,begin(), words.end(), std::bind1st(pointee_is_equal, word_to_be_found));
in c++11 becomes easier, lambdas:
auto matching_iter = std::find_if(words,begin(), words.end(), [&word_to_be_found](const std::string* p) { return word_to_be_found == *p; });
Comments
Post a Comment