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



描述

C++ 函式std::multimap::multimap() 透過初始化列表構造一個多對映。

宣告

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

C++11

multimap (initializer_list<value_type> il,
          const key_compare& comp = key_compare(),
          const allocator_type& alloc = allocator_type());

引數

  • il - 初始化列表。

  • comp - 一個二元謂詞,它接受兩個鍵作為引數,如果第一個引數在第二個引數之前則返回 true,否則返回 false。預設情況下,它使用 less 謂詞。

  • alloc - 分配器物件。

返回值

建構函式不返回值。

異常

如果丟擲異常,對容器沒有影響。

時間複雜度

線性,即 O(n)

示例

以下示例演示了 std::multimap::multimap() 函式的使用。

#include <iostream>
#include <map>

using namespace std;

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

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

   for (auto it = m.begin(); it != m.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
廣告