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 | - | 要交换内容的 tuple
|
返回值
(无)
异常
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 算法 (函数模板) |