C++ map 根据value找key
2017-06-01 17:02
1196 查看
C++ map 根据value找key
flyfish
测试所需头文件
初始
根据key 找 value
根据value 找key lambda方式
根据value 找key 函数对象方式
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; }
相关文章推荐
- 根据表单的name(名字)数组,返回对应的Map对象。Map对象的key是表单的name,Map对象的value是表单的value
- C++ 10.3 关联容器map定义以及value_type、key_type、mapped_type
- Map相关 - 根据value获取key
- 算法实战:根据Key或Value对Map进行排序及其应用
- C++ map通过key获取value
- Java 中 Map 根据键值 (key) 或者值 (value) 进行排序实现
- C++: 根据key或value对map排序
- Java中Map根据键值(key)或者值(value)进行排序实现
- C++之map插入数据相同的key不能覆盖value解决办法
- Map 根据value 获取key
- 在map中根据value获取key
- 获取map中根据key进行排序的value数据
- Map排序,获取map的第一值,根据value取key等操作(数据预处理)
- Map中根据value排序---输出value最大时所对应的Key
- c++容器map中根据key求得对应value值
- C++:基于标准容器map的多线程安全的map容器(自己的类作key和value时,慎用)
- Java中Map根据键值(key)或者值(value)进行排序实现
- 从标准输入中读取两个字符串 name:表示姓名,hobby:爱好;name作为key;hobby作为Value存入Map 然后根据姓名取得Value并打印,再根据name值删除map中的数据再打印ma
- Java中Map根据键值(key)或者值(value)进行排序实现
- 在map中根据value获取key