linux c++ map的find 和insert给出使用例子
使用例子如下:
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap;
// 插入元素
myMap.insert(std::make_pair(1, "One"));
myMap.insert(std::make_pair(2, "Two"));
myMap.insert(std::make_pair(3, "Three"));
// 查找元素
std::map<int, std::string>::iterator it = myMap.find(2);
if (it != myMap.end()) {
std::cout << "Key 2 found. Value: " << it->second << std::endl;
} else {
std::cout << "Key 2 not found" << std::endl;
}
return 0;
}
在上面的例子中,我们首先创建了一个std::map<int, std::string>类型的myMap对象。然后,我们使用insert函数插入了一些键值对。接下来,我们使用find函数查找键为2的元素。如果找到了该元素,则输出其对应的值;否则,输出未找到的提示信息。
原文地址: https://www.cveoy.top/t/topic/i4PB 著作权归作者所有。请勿转载和采集!