linux cc++ map中erase 用法c++标准为c++98给出例子
在C++98标准中,可以使用std::map的erase函数来删除map中的元素。erase函数有多个重载形式,可以根据不同的需求选择合适的重载形式。
下面是一个使用erase函数删除map中元素的例子:
#include <iostream>
#include <map>
int main() {
std::map<int, std::string> myMap;
// 添加一些元素到map中
myMap[1] = "apple";
myMap[2] = "banana";
myMap[3] = "orange";
// 删除键为2的元素
myMap.erase(2);
// 遍历map并打印剩余的元素
for (std::map<int, std::string>::iterator it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
return 0;
}
输出结果:
1: apple
3: orange
在上面的例子中,我们首先创建了一个std::map对象myMap,然后使用[]操作符将一些键值对添加到map中。然后,我们使用erase函数删除键为2的元素。最后,我们使用迭代器遍历map并打印剩余的元素。可以看到,键为2的元素已被成功删除
原文地址: https://www.cveoy.top/t/topic/isUB 著作权归作者所有。请勿转载和采集!