map::erase
描述 (Description)
C ++函数std::map::erase()从地图中删除元素范围。
此成员函数减小了地图的大小。
声明 (Declaration)
以下是std :: map :: erase()函数形式std :: map头的声明。
C++98
void erase (iterator first, iterator last);
参数 (Parameters)
first - 将迭代器输入到范围内的初始位置。
last - 将迭代器输入到范围内的最终位置。
返回值
没有
异常 (Exceptions)
该成员函数不会抛出异常。
时间复杂
在第一个到最后一个之间的距离线性。
例子 (Example)
以下示例显示了std :: map :: erase()函数的用法。
#include <iostream>
#include <map>
using namespace std;
int main(void) {
/* Initializer_list constructor */
map<char, int> m = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5},
};
cout << "Map contains following elements befor erase operation" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " = " << it->second << endl;
auto it = m.begin();
++it, ++it;
m.erase(m.begin(), it);
cout << "Map contains following elements after erase operation" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Map contains following elements befor erase operation
a = 1
b = 2
c = 3
d = 4
e = 5
Map contains following elements after erase operation
c = 3
d = 4
e = 5