function template
<unordered_map>

std::swap (unordered_multimap)

template <class Key, class T, class Hash, class Pred, class Alloc>  void swap ( unordered_multimap<Key,T,Hash,Pred,Alloc>& lhs,              unordered_multimap<Key,T,Hash,Pred,Alloc>& rhs );
Exchanges contents of two unordered_multimap containers
容器lhs的內容與rhs的內容交換。兩個容器物件必須是相同的型別(相同的模板引數),但大小可能不同。

After the call to this member function, the elements in lhs are those which were in rhs before the call, and the elements of rhs are those which were in lhs. Other objects kept internally by the containers (such as theirhasherorkey_equal物件)也會被交換。

這是通用演算法 swap 的一個特化,它透過交換指向資料的內部指標來提高其效能,而無需實際對單個元素執行任何複製或移動操作。

引數

lhs,rhs
unordered_multimap containers (to the left- and right-hand side of the operator, respectively), having both the same template parameters (, T, Hash, PredAlloc).

返回值



示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// swap (unordered_multimap specialization)
#include <iostream>
#include <string>
#include <unordered_map>

int main ()
{
  std::unordered_multimap<std::string,std::string>
     a = {{"orange","FL"},{"apple","NY"},{"apple","WA"}},
     b  = {{"strawberry","LA"},{"strawberry","NC"},{"pear","OR"}};

  swap(a,b);

  std::cout << "a: ";
  for (auto& x: a) std::cout << " " << x.first << ":" << x.second;
  std::cout << std::endl;

  std::cout << "b: ";
  for (auto& x: b) std::cout << " " << x.first << ":" << x.second;
  std::cout << std::endl;

  return 0;
}

可能的輸出
a:  pear:OR strawberry:LA strawberry:NC
b:  orange:FL apple:NY apple:WA


複雜度

常量。

迭代器有效性

所有迭代器、指標和引用都保持有效,但現在它們指向其他容器中的元素,並在其中進行迭代。

另見