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;
        }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM