//删除 map 容器中指定位置的键值对
iterator erase (const_iterator position);
注意,如果删除的键值对位于 map 容器中最后的位置,则该方法返回的迭代器指向的是最后一个键值对之后的位置(等同于 end() 成员方法返回的迭代器)。
下面程序演示了 erase() 成员方法的用法:
#include <iostream>
#include <map> // map
#include <string> // string
using namespace std;
int main() {
//创建并初始化 map 容器
map<std::string, std::string>myMap{ {"C语言教程", "http://c.biancheng.net/c/"},
{"Python教程", "http://c.biancheng.net/python/"},
{"STL教程", "http://c.biancheng.net/stl/"} };
//输出myMap容器存储键值对的个数
cout <<"myMap size = " << myMap.size() << endl;
//创建一个指向要删除的键值对的迭代器
map<string, string>::iterator iter = ++myMap.begin();
//执行删除操作
map<string, string>::iterator ret = myMap.erase(iter);
//输出当前 myMap 容器中剩余键值对的个数
cout << "new myMap size = " << myMap.size() << endl;
//输出 erase() 方法返回的迭代器指向的键值对
cout << ret->first << " " << ret->second << endl;
return 0;
}
程序执行结果为:
myMap size = 3
new myMap size = 2
STL教程 http://c.biancheng.net/stl/
//删除 map 容器中键为 k 的键值对
size_type erase (const key_type& k);
#include <iostream>
#include <map> // map
#include <string> // string
using namespace std;
int main() {
//创建并初始化 map 容器
map<std::string, std::string>myMap{ {"C语言教程", "http://c.biancheng.net/c/"},
{"Python教程", "http://c.biancheng.net/python/"},
{"STL教程", "http://c.biancheng.net/stl/"} };
//输出myMap容器存储键值对的个数
cout <<"myMap size = " << myMap.size() << endl;
//删除 myMap 容器键为 "Python教程" 的键值对
int num = myMap.erase("Python教程");
//输出 myMap 容器中剩余键值对的数量
cout << "new myMap size = " << myMap.size() << endl;
//输出 erase() 方法的返回值
cout << "num = " << num << endl;
return 0;
}
程序执行结果为:
myMap size = 3
new myMap size = 2
num = 1
//删除 map 容器中位于 [first,last) 区域内的所有键值对
iterator erase (const_iterator first, const_iterator last);
举个例子:注意,如果 [fist,last) 范围之后,不再有任何键值对,则 erase() 方法返回的迭代器将指向 map 容器最后一个键值对之后的位置(等同于 end() 成员方法返回的迭代器)。
#include <iostream>
#include <map> // map
#include <string> // string
using namespace std;
int main() {
//创建并初始化 map 容器
map<std::string, std::string>myMap{ {"C语言教程", "http://c.biancheng.net/c/"},
{"Python教程", "http://c.biancheng.net/python/"},
{"STL教程", "http://c.biancheng.net/stl/"} };
//输出myMap容器存储键值对的个数
cout <<"myMap size = " << myMap.size() << endl;
map<string, string>::iterator first = myMap.begin();
map<string, string>::iterator last = --myMap.end();
map<string, string>::iterator ret = myMap.erase(first,last);
cout << "new myMap size = " << myMap.size() << endl;
cout << ret->first << " " << ret->second << endl;
return 0;
}
程序执行结果为:
myMap size = 3
new myMap size = 1
STL教程 http://c.biancheng.net/stl/
注意,[first,last) 为半闭半开区间,这意味着 erase() 方法会从 first 指向的键值对开始删除(包括 first 指向的键值对),一直删到 last 指向键值对的前面那个键值对。
void clear()
可以看到,该方法不需要传入任何参数,也不需要接收任何返回值。
#include <iostream>
#include <map> // map
#include <string> // string
using namespace std;
int main() {
//创建并初始化 map 容器
map<std::string, std::string>myMap{ {"C语言教程", "http://c.biancheng.net/c/"},
{"Python教程", "http://c.biancheng.net/python/"},
{"STL教程", "http://c.biancheng.net/stl/"} };
//输出myMap容器存储键值对的个数
cout <<"myMap size = " << myMap.size() << endl;
//清除 myMap 容器中所有的键值对
myMap.clear();
cout << "new myMap size = " << myMap.size() << endl;
return 0;
}
程序执行结果为:
myMap size = 3
new myMap size = 0
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有