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



描述

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

宣告

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

C++98

template <class Key, class T, class Compare, class Alloc>
   void swap (multimap<Key,T,Compare,Alloc>& first, 
      multimap<Key,T,Compare,Alloc>& second);

引數

  • first − 第一個 multimap 物件。

  • second − 第二個相同型別的 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;

   swap(m1, m2);

   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
廣告
© . All rights reserved.