當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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


描述

C++ 函數std::multimap::multimap()使用移動語義構造一個包含 other 內容的多重映射。

聲明

以下是 std::multimap::multimap() 函數形式 std::map 頭的聲明。

C++11

multimap (multimap&& other);
multimap (multimap&& other, const allocator_type& alloc);

參數

  • other- 另一個相同類型的多映射對象。

  • 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(move(m1));

   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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。