map按value查找相应元素

find_if算法用来在map中查找value符合条件的pair元素,返回指向该符合条件元素的迭代器,如果找到,那么返回最后一个元素的后一个元素end();

1.首先要定义头文件
 #include <algorithm>

2.定义一个比较函数

class map_finder
{
public:
    map_finder(const std::wstring &cmp_wstring) :m_s_cmp_string(cmp_wstring){}
    bool operator ()(const std::map<int, std::wstring>::value_type &pair)
    {
            return pair.second == m_s_cmp_string;
    }
private:
        const std::wstring &m_s_cmp_string;
};

3.查找

 map<int, wstring>::iterator it = m_listCtrl.m_secLineText.end();
       wstring s = str.GetString();
       it = std::find_if(m_listCtrl.m_secLineText.begin(), m_listCtrl.m_secLineText.end(), map_finder(s));
         if (it != m_listCtrl.m_secLineText.end())
        {
            int a = it->first;
            cout << " find!!!! " << endl;
        }
        else
        {
            cout << "Not find!!!! " << endl;
        }

posted @ 2017-07-04 08:44  ye_ming  阅读(3885)  评论(0编辑  收藏  举报