C++ map 根據(jù)value找key的實(shí)現(xiàn)
更新時(shí)間:2019年12月19日 11:11:50 作者:flyfish1986
今天小編就為大家分享一篇C++ map 根據(jù)value找key的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
flyfish
測(cè)試所需頭文件
#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"));
根據(jù)key 找 value
std::string s = "";
auto it = t.find(2);
if (it != t.end())
{
s = (*it).second;
}
根據(jù)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;
}
根據(jù)value 找key 函數(shù)對(duì)象方式
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_;
};
//調(diào)用
int n = 0;
auto it = std::find_if(t.begin(), t.end(), finder("d"));
if (it != t.end())
{
n = (*it).first;
}
以上這篇C++ map 根據(jù)value找key的實(shí)現(xiàn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- C++ map用法總結(jié)(整理)
- C++保存HBITMAP為位圖文件的實(shí)現(xiàn)方法
- c++ map索引不存在的key可能導(dǎo)致的后果分析
- C++利用map實(shí)現(xiàn)并查集
- c++容器list、vector、map、set區(qū)別與用法詳解
- C++中rapidjson將嵌套map轉(zhuǎn)為嵌套json的講解
- C++中rapidjson將map轉(zhuǎn)為json的方法
- C++中rapidjson組裝map和數(shù)組array的代碼示例
- C++中map和vector作形參時(shí)如何給定默認(rèn)參數(shù)?
- c++ 數(shù)據(jù)結(jié)構(gòu)map的使用詳解
相關(guān)文章
C++詳解如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)組
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)動(dòng)態(tài)數(shù)組的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
C++編程中隊(duì)內(nèi)聯(lián)函數(shù)的理解和使用
這篇文章主要介紹了C++編程中隊(duì)內(nèi)聯(lián)函數(shù)的理解和使用,簡(jiǎn)單舉例講解了inline關(guān)鍵字引出的內(nèi)聯(lián)函數(shù)的相關(guān)知識(shí),需要的朋友可以參考下2016-01-01
C語言 數(shù)據(jù)結(jié)構(gòu)平衡二叉樹實(shí)例詳解
這篇文章主要介紹了C語言 數(shù)據(jù)結(jié)構(gòu)平衡二叉樹實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06

