C++ 无序多映射库 - insert() 函数



描述

C++ 函数std::unordered_multimap::insert() 通过从初始化列表插入新元素来扩展无序多映射。此函数使容器大小增加一。

声明

以下是来自std::unordered_map() 头文件的 std::unordered_multimap::insert() 函数声明。

C++11

void insert(initializer_list<value_type> il);

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

参数

il − 初始化列表。

返回值

时间复杂度

平均情况下为常数,即 O(1)。

最坏情况下为线性,即 O(n)。

示例

以下示例演示了 std::unordered_multimap::insert() 函数的使用。

#include <iostream>
#include <unordered_map>

using namespace std;

int main(void) {
   unordered_multimap<char, int> umm = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            };

   umm.insert({{'d', 4}, {'e', 5}});

   cout << "Unordered multimap contains following elements" << endl;

   for (auto it = umm.begin(); it != umm.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

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

Unordered multimap contains following elements
e = 5
d = 4
c = 3
a = 1
b = 2
unordered_map.htm
广告