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



描述

C++ 函式std::multimap::swap() 交換 multimap 的內容與另一個 multimap 的內容。x.

宣告

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

C++98

void swap (multimap& x);

引數

x - 另一個相同型別的 multimap 物件。

返回值

異常

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

時間複雜度

常數,即 O(1)

示例

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

#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},
            {'d', 5}
         };

   multimap<char, int> m2;

   m2.swap(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
d = 5
map.htm
廣告