std::unordered_set<Key,Hash,KeyEqual,Allocator>::swap
std::unordered_set
成员函数 | ||||
迭代器 | ||||
容量 | ||||
修改器 | ||||
unordered_set::swap
|
||||
(C++17)
|
||||
(C++17)
|
||||
查找 | ||||
(C++20)
|
||||
桶接口 | ||||
哈希策略 | ||||
观察器 | ||||
非成员函数 | ||||
(C++20
前)
|
||||
(C++20)
|
||||
推导指引(C++17) |
void swap( unordered_set& other
);
|
(C++11 起) (C++17 前) |
|
void swap( unordered_set& other
) noexcept(/* 见下文 */);
|
(C++17 起) | |
将内容与 other 的交换。不在单独的元素上调用任何移动、复制或交换操作。
所有迭代器和引用保持有效。end() 迭代器会失效。
Hash
和 KeyEqual
对象必须可交换 (Swappable)
,并用非成员 swap
的非限定调用交换它们。
如果 std::allocator_traits<allocator_type>::propagate_on_container_swap::value 是
true,那么就会用对非成员 |
(C++11 起) |
参数
other | - | 要与之交换内容的容器 |
返回值
(无)
异常
任何 |
(C++17 前) |
noexcept 说明:
noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_swappable<Hash>::value |
(C++17 起) |
复杂度
常数。
示例
#include <iostream> #include <unordered_set> template<class Os, class Co> Os& operator<<(Os& os, const Co& co) { os << "{"; for (auto const& i : co) os << ' ' << i; return os << " } "; } int main() { std::unordered_set<int> a1{3, 1, 3, 2}, a2{5, 4, 5}; auto it1 = std::next(a1.begin()); auto it2 = std::next(a2.begin()); const int& ref1 = *(a1.begin()); const int& ref2 = *(a2.begin()); std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n'; a1.swap(a2); std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n'; // 注意交换前指代一个容器中的元素的每个迭代器在交换后都指代同一元素。对于引用也是这样。 }
可能的输出:
{ 2 1 3 } { 4 5 } 1 5 2 4 { 4 5 } { 2 1 3 } 1 5 2 4
参阅
(C++11)
|
特化 std::swap 算法 (函数模板) |