国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ map 根據value找key的實現

2020-01-26 13:21:43
字體:
來源:轉載
供稿:網友

flyfish

測試所需頭文件

#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的實現就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平定县| 延寿县| 永嘉县| 安阳县| 新民市| 蚌埠市| 榆林市| 高清| 榆林市| 合江县| 武宁县| 宿州市| 两当县| 扶沟县| 泰宁县| 旌德县| 垣曲县| 新乡县| 涪陵区| 襄垣县| 新巴尔虎右旗| 穆棱市| 珠海市| 余江县| 乌审旗| 丰城市| 密山市| 太保市| 共和县| 苏尼特左旗| 绥阳县| 黔西县| 新绛县| 塔城市| 枣庄市| 周口市| 和平县| 应城市| 阳新县| 商城县| 洪湖市|