linux cc++ map key为string value 为int 给出insert 和erase 用法c++标准为c++98给出例子
在C++98中,可以使用std::map容器来实现key为string,value为int的映射。以下是std::map的insert和erase的用法示例:
#include <iostream>
#include <map>
int main() {
std::map<std::string, int> myMap;
// 使用 insert() 插入键值对
myMap.insert(std::make_pair("apple", 2));
myMap.insert(std::make_pair("banana", 3));
myMap.insert(std::make_pair("orange", 4));
// 使用 erase() 删除键值对
myMap.erase("banana");
// 遍历输出键值对
std::map<std::string, int>::iterator it;
for (it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;
}
return 0;
}
输出结果:
Key: apple, Value: 2
Key: orange, Value: 4
在上面的例子中,首先创建了一个std::map对象myMap,然后使用insert()方法插入了三个键值对,分别是"apple"对应的值为2,"banana"对应的值为3,"orange"对应的值为4。接着使用erase()方法删除了键为"banana"的键值对。最后使用迭代器遍历输出了剩余的键值对
原文地址: https://www.cveoy.top/t/topic/isUK 著作权归作者所有。请勿转载和采集!