C++ 對映庫 - multimap() 函式



描述

C++ 函式std::multimap::multimap()構造一個多對映,其中包含現有容器中每個元素的副本。其他.

宣告

以下是來自std::map標頭檔案的std::multimap::multimap()函式的宣告。

C++98

multimap (const multimap& other);

C++11

multimap (const multimap& other);
multimap (const 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(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
map.htm
廣告
© . All rights reserved.