小編給大家分享一下C++ map如何實現根據value找key,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

測試所需頭文件
#include <algorithm> #include <vector> #include <map> #include <string>
初始
std::map<int, std::string> t; t.insert(std::make_pair(1, "a")); t.insert(std::make_pair(2, "b")); t.insert(std::make_pair(3, "c")); t.insert(std::make_pair(4, "d"));
根據key 找 value
std::string s = "";
auto it = t.find(2);
if (it != t.end())
{
s = (*it).second;
}根據value 找key lambda方式
std::string s = "c";
auto find_item = std::find_if(t.begin(), t.end(),
[s](const std::map<int, std::string>::value_type item)
{
return item.second == s;
});
int n = 0;
if (find_item!= t.end())
{
n = (*find_item).first;
}根據value 找key 函數對象方式
class finder
{
public:
finder(const std::string &cmp_string) :s_(cmp_string){}
bool operator ()(const std::map<int, std::string>::value_type &item)
{
return item.second == s_;
}
private:
const std::string &s_;
};
//調用
int n = 0;
auto it = std::find_if(t.begin(), t.end(), finder("d"));
if (it != t.end())
{
n = (*it).first;
}看完了這篇文章,相信你對“C++ map如何實現根據value找key”有了一定的了解,如果想了解更多相關知識,歡迎關注創新互聯網站建設公司行業資訊頻道,感謝各位的閱讀!
另外有需要云服務器可以了解下創新互聯建站www.yijiale78.com,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網頁題目:C++map如何實現根據value找key-創新互聯
轉載注明:http://www.yijiale78.com/article12/icodc.html
成都網站建設公司_創新互聯,為您提供企業建站、靜態網站、App開發、移動網站建設、響應式網站、云服務器
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯