function template
<unordered_set>
std::swap (unordered_set)
template <class Key, class Hash, class Pred, class Alloc> void swap ( unordered_set<Key,Hash,Pred,Alloc>& lhs, unordered_set<Key,Hash,Pred,Alloc>& rhs );
Exchanges contents of two unordered_set 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_set containers (to the left- and right-hand side of the operator, respectively), having both the same template parameters (Key, Hash, Pred and Alloc).
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_set specialization)
#include <iostream>
#include <string>
#include <unordered_set>
int main ()
{
std::unordered_set<std::string>
first = {"Metropolis","Solaris","Westworld"},
second = {"Avatar","Inception"};
swap(first,second);
std::cout << "first:";
for (const std::string& x: first) std::cout << " " << x;
std::cout << std::endl;
std::cout << "second:";
for (const std::string& x: second) std::cout << " " << x;
std::cout << std::endl;
return 0;
}
|
Possible output:
first: Inception Avatar
second: Westworld Metropolis Solaris
|
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_set::swap
- Swap content (public member function)
- swap
- Exchange values of two objects (function template)
- swap_ranges
- Exchange values of two ranges (function template)