std::inplace_vector<T,N>:: swap
From cppreference.net
<
cpp
|
container
|
inplace vector
C++
Containers library
|
(C++17)
|
||||
| Sequence | ||||
|
(C++11)
|
||||
|
(C++26)
|
||||
|
(C++26)
|
||||
|
(C++11)
|
||||
| Associative | ||||
| Unordered associative | ||||
|
(C++11)
|
||||
|
(C++11)
|
||||
|
(C++11)
|
||||
|
(C++11)
|
||||
| Adaptors | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
| Views | ||||
|
(C++20)
|
||||
|
(C++23)
|
||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
std::inplace_vector
| Member types | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Non-member functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
constexpr
void
swap
(
inplace_vector
&
other
)
noexcept
(
/* 见下文 */
)
;
|
(自 C++26 起) | |
交换容器内容与 other 的内容。不会导致迭代器和引用与另一容器关联。
目录 |
参数
| 其他 | - | 用于交换内容的容器 |
返回值
(无)
异常
noexcept
规范:
noexcept
(
N
==
0
||
( std:: is_nothrow_swappable_v < T > && std:: is_nothrow_move_constructible_v < T > ) )
( std:: is_nothrow_swappable_v < T > && std:: is_nothrow_move_constructible_v < T > ) )
复杂度
与容器大小呈线性关系。
示例
运行此代码
#include <inplace_vector> #include <print> int main() { std::inplace_vector<int, 3> a1{1, 2, 3}, a2{4, 5, 6}; auto i1 = a1.begin(); auto i2 = a2.begin(); int& r1 = a1[1]; int& r2 = a2[1]; auto print_them_all = [&](auto rem) { std::println("{}a1 = {}, a2 = {}, *i1 = {}, *i2 = {}, r1 = {}, r2 = {}", rem, a1, a2, *i1, *i2, r1, r2); }; print_them_all("Before swap:\n"); a1.swap(a2); print_them_all("After swap:\n"); // 注意交换后迭代器和引用仍与其原始位置保持关联,例如 i1 指向元素 a1[0],r1 引用 a1[1]。 }
输出:
Before swap: a1 = [1, 2, 3], a2 = [4, 5, 6], *i1 = 1, *i2 = 4, r1 = 2, r2 = 5 After swap: a1 = [4, 5, 6], a2 = [1, 2, 3], *i1 = 4, *i2 = 1, r1 = 5, r2 = 2
参见
|
(C++26)
|
特化
std::swap
算法
(函数模板) |