std::map find_if condition style confusion

前端 未结 8 1083
星月不相逢
星月不相逢 2021-02-01 23:43

I\'d like to use std::find_if to search for the first element in my map that has a certain value in a specific element of its value structure. I\'m a little confus

8条回答
  •  庸人自扰
    2021-02-02 00:03

    Building on all the answers above I cheat by using decltype with C++11 semantics.

    auto beg_ = myMap.begin();
    auto end_ = myMap.end();
    auto it = find_if(beg_, end_,
        [&some_val](decltype(*beg_) & vt) {
            return vt.second == some_val;});
    if (end_ != it) {
        auto key_found = (*it).first;
    } else {
        // throw error not found.
    }
    

提交回复
热议问题