目录

unordered_map::erase

描述 (Description)

C ++函数std::unordered_map::equal()返回与特定键匹配的元素范围。

在unordered_map容器中,键是唯一的,范围最多包含一个元素。 如果k与容器中的任何键都不匹配,则返回的范围以其下限和上限范围结束。

声明 (Declaration)

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

C++11

pair<iterator,iterator> equal_range(const key_type& k);
pair<const_iterator,const_iterator> equal_range(const key_type& k) const;

参数 (Parameters)

k - 要搜索的密钥。

描述 (Description)

C ++函数std::unordered_map::erase()位置移除unordered_map的单个元素。

此成员函数将unordered_map的大小减小一。

声明 (Declaration)

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

C++11

iterator erase(const_iterator position);

参数 (Parameters)

position - 要移除的元素的迭代器。

返回值

返回一个迭代器,指向紧跟最后一个元素擦除后的位置。

时间复杂

常数即O(1)

例子 (Example)

以下示例显示了std :: unordered_map :: erase()函数的用法。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<char, int> um = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5}
            };
   cout << "Unordered map contains following elements before erase operation" 
      << endl;
   for (auto it = um.begin(); it != um.end(); ++it)
      cout << it->first << " = " << it->second 
         << endl;
   um.erase(um.begin());
   cout << endl;
   cout << "Unordered map contains following elements after erase operation" 
      << endl;
   for (auto it = um.begin(); it != um.end(); ++it)
      cout << it->first << " = " << it->second << endl;
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Unordered map contains following elements before erase operation
e = 5
a = 1
b = 2
c = 3
d = 4
Unordered map contains following elements after erase operation
a = 1
b = 2
c = 3
d = 4
↑回到顶部↑
WIKI教程 @2018