当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


C++ Map multimap()用法及代码示例


描述

C++ 函数std::multimap::multimap()构造一个包含与范围内一样多的元素的多重映射第一的最后的

声明

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

C++98

template <class InputIterator>
multimap (InputIterator first, InputIterator last,
          const key_compare& comp = key_compare(),
          const allocator_type& alloc = allocator_type());

C++11

template <class InputIterator>
multimap (InputIterator first, InputIterator last,
          const key_compare& comp = key_compare(),
          const allocator_type& = allocator_type());

参数

  • first− 将迭代器输入到初始位置。

  • last− 将迭代器输入到最终位置。

  • comp- 一个二元谓词,它接受两个关键参数,如果第一个参数在第二个参数之前,则返回真,否则返回假。默认情况下,它使用 less 谓词。

  • alloc− 分配器对象。

返回值

构造函数从不返回值。

异常

如果抛出异常,对容器没有影响。

时间复杂度

线性,即 O(n)

示例

下面的例子展示了 std::multimap::multimap() 函数的用法。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   /* Multimap with duplicates */
   multimap<char, int> m1 = {
         {'a', 1},
         {'a', 2},
         {'b', 3},
         {'c', 4},
         {'c', 5},
               };

   multimap<char, int>m2(m1.begin(), m1.end());

   cout << "Multimap contains following elements:" << endl;

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

   return 0;
}

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

Multimap contains following elements:
a = 1
a = 2
b = 3
c = 4
c = 5

相关用法


注:本文由纯净天空筛选整理自 C++ Map Library - multimap() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。