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