庆幸的是,map 容器的类模板中提供了以下 2 种方法,可直接获取 map 容器指定键对应的值。注意,使用 map 容器存储的各个键值对,其键的值都是唯一的,因此指定键对应的值最多有 1 个。
[ ]
运算符进行了重载,这意味着,类似于借助数组下标可以直接访问数组中元素,通过指定的键,我们可以轻松获取 map 容器中该键对应的值。#include <iostream> #include <map> // map #include <string> // string using namespace std; int main() { //创建并初始化 map 容器 std::map<std::string, std::string>myMap{ {"STL教程","http://c.biancheng.net/stl/"}, {"C语言教程","http://c.biancheng.net/c/"}, {"Java教程","http://c.biancheng.net/java/"} }; string cValue = myMap["C语言教程"]; cout << cValue << endl; return 0; }程序执行结果为:
http://c.biancheng.net/c/
可以看到,在第 11 行代码中,通过指定键的值为 "C语言教程",借助重载的 [ ] 运算符,就可以在 myMap 容器中直接找到该键对应的值。#include <iostream> #include <map> // map #include <string> // string using namespace std; int main() { //创建空 map 容器 std::map<std::string, int>myMap; int cValue = myMap["C语言教程"]; for (auto i = myMap.begin(); i != myMap.end(); ++i) { cout << i->first << " "<< i->second << endl; } return 0; }程序执行结果为:
C语言教程 0
显然,对于空的 myMap 容器来说,其内部没有以 "C语言教程" 为键的键值对,这种情况下如果使用 [ ] 运算符获取该键对应的值,其功能就转变成了向该 myMap 容器中添加一个<"C语言教程",0>
键值对(由于 myMap 容器规定各个键值对的值的类型为 int,该类型的默认值为 0)。#include <iostream> #include <map> // map #include <string> // string using namespace std; int main() { //创建空 map 容器 std::map<string, string>myMap; myMap["STL教程"]="http://c.biancheng.net/java/"; myMap["Python教程"] = "http://c.biancheng.net/python/"; myMap["STL教程"] = "http://c.biancheng.net/stl/"; for (auto i = myMap.begin(); i != myMap.end(); ++i) { cout << i->first << " " << i->second << endl; } return 0; }程序执行结果为:
Python教程 http://c.biancheng.net/python/
STL教程 http://c.biancheng.net/stl/
#include <iostream> #include <map> // map #include <string> // string using namespace std; int main() { //创建并初始化 map 容器 std::map<std::string, std::string>myMap{ {"STL教程","http://c.biancheng.net/stl/"}, {"C语言教程","http://c.biancheng.net/c/"}, {"Java教程","http://c.biancheng.net/java/"} }; cout << myMap.at("C语言教程") << endl; //下面一行代码会引发 out_of_range 异常 //cout << myMap.at("Python教程") << endl; return 0; }程序执行结果为:
http://c.biancheng.net/c/
程序第 12 行代码处,通过 myMap 容器调用 at() 成员方法,可以成功找到键为 "C语言教程" 的键值对,并返回该键对应的值;而第 14 行代码,由于当前 myMap 容器中没有以 "Python教程" 为键的键值对,会导致 at() 成员方法查找失败,并抛出 out_of_range 异常。#include <iostream> #include <map> // map #include <string> // string using namespace std; int main() { //创建并初始化 map 容器 std::map<std::string, std::string>myMap{ {"STL教程","http://c.biancheng.net/stl/"}, {"C语言教程","http://c.biancheng.net/c/"}, {"Java教程","http://c.biancheng.net/java/"} }; map< std::string, std::string >::iterator myIter = myMap.find("C语言教程"); cout << myIter->first << " " << myIter->second << endl; return 0; }程序执行结果为:
C语言教程 http://c.biancheng.net/c/
注意,此程序中如果 find() 查找失败,会导致第 13 行代码运行出错。因为当 find() 方法查找失败时,其返回的迭代器指向的是容器中最后一个键值对之后的位置,即不指向任何有意义的键值对,也就没有所谓的 first 和 second 成员了。
#include <iostream> #include <map> // map #include <string> // string using namespace std; int main() { //创建并初始化 map 容器 std::map<std::string, std::string>myMap{ {"STL教程","http://c.biancheng.net/stl/"}, {"C语言教程","http://c.biancheng.net/c/"}, {"Java教程","http://c.biancheng.net/java/"} }; for (auto iter = myMap.begin(); iter != myMap.end(); ++iter) { //调用 string 类的 compare() 方法,找到一个键和指定字符串相同的键值对 if (!iter->first.compare("C语言教程")) { cout << iter->first << " " << iter->second << endl; } } return 0; }程序执行结果为:
C语言教程 http://c.biancheng.net/c/
本节所介绍的几种方法中,仅从“在 map 容器存储的键值对中,获取指定键对应的值”的角度出发,更推荐使用 at() 成员方法,因为该方法既简单又安全。
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有