unordered_map::erase
描述 (Description)
C ++函数std::unordered_multimap::erase()从unordered_multimap中删除元素范围。
声明 (Declaration)
以下是std :: unordered_multimap :: erase()函数形式std :: unordered_map()头的声明。
C++11
iterator erase (const_iterator first, const_iterator last);
参数 (Parameters)
first - 将迭代器输入到范围内的初始位置。
last - 将迭代器输入到范围内的最终位置。
返回值
返回最后一个删除元素后的迭代器。
时间复杂
线性即O(n)
例子 (Example)
以下示例显示了std :: unordered_multimap :: erase()函数的用法。
#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
unordered_multimap<char, int> umm = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5}
};
cout << "Unordered_multimap contains following elements "
<< "before remove operation" << endl;
for (auto it = umm.begin(); it != umm.end(); ++it)
cout << it->first << " = " << it->second << endl;
auto it = umm.begin();
++it; ++it;
umm.erase(umm.begin(), it);
cout << "Unordered_multimap contains following elements "
<< "after remove operation" << endl;
for (auto it = umm.begin(); it != umm.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Unordered_multimap contains following elements before remove operation
e = 5
a = 1
b = 2
c = 3
d = 4
Unordered_multimap contains following elements after remove operation
b = 2
c = 3
d = 4