A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/unordered_map/unordered_multimap/swap(global)/ below:

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

The contents of container lhs are exchanged with those of rhs. Both container objects must be of the same type (same template parameters), although sizes may differ.

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 their hasher or key_equal objects) are also swapped.

This is a specialization of the generic algorithm swap that improves its performance by exchanging internal pointers to data, without actually performing any copies or moves on the individual elements.



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

Return value none

Example
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;
}

Possible output:
a:  pear:OR strawberry:LA strawberry:NC
b:  orange:FL apple:NY apple:WA


Complexity Constant.

Iterator validity All iterators, pointers and references remain valid, but now are referring to elements in the other container, and iterate in it.

See also
unordered_multimap::swap
Swap content (public member function)
swap
Exchange values of two objects (function template)
swap_ranges
Exchange values of two ranges (function template)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4