C++ Unordered_multimap 库-erase() 函数


描述

C++ 函数std::unordered_multimap::erase()删除与键k关联的映射值。

宣言

以下是 std::unordered_multimap::erase() 函数形式 std::unordered_map() 标头的声明。

C++11

size_type erase(const key_type& k);

参数

k - 要删除的元素的键。

返回值

返回删除的元素数。

时间复杂度

线性即 O(n)

例子

以下示例显示了 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;

   cout << endl;

   umm.erase('e');

   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
a = 1
b = 2
c = 3
d = 4
无序_map.htm