std::swap(std::tuple)
来自 cppreference.com
定义在头文件 <tuple> 中 |
||
(1) | ||
template< class... Types > void swap( std::tuple<Types...>& lhs, |
(自 C++11 起) (直到 C++20) |
|
template< class... Types > constexpr void swap( std::tuple<Types...>& lhs, |
(自 C++20 起) | |
template< class... Types > constexpr void swap( const std::tuple<Types...>& lhs, |
(2) | (自 C++23 起) |
交换 lhs 和 rhs 的内容。等效于 lhs.swap(rhs).
(自 C++17 起) |
内容 |
[编辑] 参数
lhs, rhs | - | 要交换其内容的元组 |
[编辑] 返回值
(无)
[编辑] 异常
noexcept 规范:
noexcept(noexcept(lhs.swap(rhs)))
[编辑] 示例
运行此代码
#include <iostream> #include <string> #include <tuple> int main() { std::tuple<int, std::string, float> p1{42, "ABCD", 2.71}, p2; p2 = std::make_tuple(10, "1234", 3.14); auto print_p1_p2 = [&](auto rem) { std::cout << rem << "p1 = {" << std::get<0>(p1) << ", " << std::get<1>(p1) << ", " << std::get<2>(p1) << "}, " << "p2 = {" << std::get<0>(p2) << ", " << std::get<1>(p2) << ", " << std::get<2>(p2) << "}\n"; }; print_p1_p2("Before p1.swap(p2): "); p1.swap(p2); print_p1_p2("After p1.swap(p2): "); swap(p1, p2); print_p1_p2("After swap(p1, p2): "); }
输出
Before p1.swap(p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14} After p1.swap(p2): p1 = {10, 1234, 3.14}, p2 = {42, ABCD, 2.71} After swap(p1, p2): p1 = {42, ABCD, 2.71}, p2 = {10, 1234, 3.14}
[编辑] 另请参阅
交换两个 tuple 的内容(公共成员函数) | |
(C++11) |
专门化了 std::swap 算法 (函数模板) |