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