function template
<unordered_set>
std::swap (unordered_multiset)
template <class Key, class Hash, class Pred, class Alloc> void swap ( unordered_multiset<Key,Hash,Pred,Alloc>& lhs, unordered_multiset<Key,Hash,Pred,Alloc>& rhs );
Exchanges contents of two unordered_multiset 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_multiset containers (to the left- and right-hand side of the operator, respectively), having both the same template parameters (键, Hash, Pred和Alloc).
示例
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_multiset specialization)
#include <iostream>
#include <string>
#include <unordered_set>
int main ()
{
std::unordered_multiset<std::string>
first = {"cow","chicken","pig","pig"},
second = {"wolf","rabbit","rabbit"};
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;
}
|
可能的输出
first: wolf rabbit rabbit
second: chicken cow pig pig
|
迭代器有效性
所有迭代器、指针和引用都保持有效,但现在它们指向其他容器中的元素,并在其中进行迭代。