c++11 标准模板(STL)(std::tuple)(四)
·
定义于头文件 <tuple>
|
template< class... Types > | (C++11 起) |
类模板 std::tuple 是固定大小的异类值汇集。它是 std::pair 的推广。
|
若 (std::is_trivially_destructible_v<Types> && ...) 为 true ,则 | (C++17 起) |
模板形参
| Types... | - | tuple 所存储的元素的类型。支持空列表。 |
交换二个 tuple 的内容
std::tuple<Types...>::swap
|
void swap( tuple& other ) noexcept(/* see below */); | (C++11 起) (C++20 前) | |
|
constexpr void swap( tuple& other ) noexcept(/* see below */); | (C++20 起) |
对 *this 中的每个元素与 other 中其对应元素调用 swap (可以是 std::swap ,或通过 ADL 找到的)。
参数
| other | - | 要交换值的 tuple |
返回值
(无)
异常
noexcept 规定:
noexcept(
noexcept(swap(std::declval<T0&>>(), std::declval<T0&>())) &&
noexcept(swap(std::declval<T1&>>(), std::declval<T1&>())) &&
noexcept(swap(std::declval<T2&>>(), std::declval<T2&>())) &&
...
)
上述表达式中,以 C++17 std::is_nothrow_swappable 特性所用的相同方式查找标识符 swap 。(C++17 前)
noexcept 规定:
noexcept(
std::is_nothrow_swappable_v<T0> &&
std::is_nothrow_swappable_v<T1> &&
std::is_nothrow_swappable_v<T2> &&
...
) (C++17 起)
特化 std::swap 算法
std::swap(std::tuple)
|
template< class... Types > | (C++11 起) (C++20 前) | |
| template< class... Types >
constexpr void swap( tuple<Types...>& lhs, tuple<Types...>& rhs ) noexcept(/* see below */); | (C++20 起) |
交换 lhs 与 rhs 的内容。等价于 lhs.swap(rhs) 。
|
此函数不参与重载决议,除非 std::is_swappable_v<Ti> 对来自从 0 到 | (C++17 起) |
参数
| lhs, rhs | - | 要交换内容的 tuple |
返回值
(无)
调用示例
#include <tuple>
#include <iostream>
#include <string>
#include <typeinfo>
#include <stdexcept>
#include <iostream>
#include <vector>
#include <memory>
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell(const Cell &cell)
{
x = cell.x;
y = cell.y;
}
Cell &operator+(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator+=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator*=(int n)
{
x *= n;
y *= n;
return *this;
}
Cell &operator++()
{
x += 1;
y += 1;
return *this;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
// 打印任何大小 tuple 的辅助函数
template<class Tuple, std::size_t N>
struct TuplePrinter
{
static void print(const Tuple& t)
{
TuplePrinter < Tuple, N - 1 >::print(t);
std::cout << ", " << std::get < N - 1 > (t);
}
};
template<class Tuple>
struct TuplePrinter<Tuple, 1>
{
static void print(const Tuple& t)
{
std::cout << std::get<0>(t);
}
};
template<class... Args>
void print(const std::tuple<Args...>& t)
{
std::cout << "(";
TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
std::cout << ")" << std::endl;
}
// 辅助函数结束
int main()
{
std::cout << "before swap: " << std::endl;
std::tuple<Cell, std::string, double> tuple1(Cell{101, 101}, "GGX", 3.14);
std::cout << "tuple1: ";
print(tuple1);
std::tuple<Cell, std::string, double> tuple2(Cell{202, 200}, "XGG", 3.1415);
std::cout << "tuple2: ";
print(tuple2);
std::cout << std::endl;
//对 *this 中的每个元素与 other 中其对应元素调用 swap (可以是 std::swap ,或通过 ADL 找到的)。
tuple1.swap(tuple2);
std::cout << "after swap: " << std::endl;
std::cout << "tuple1: ";
print(tuple1);
std::cout << "tuple2: ";
print(tuple2);
std::cout << std::endl;
//特化 std::swap 算法
//交换 lhs 与 rhs 的内容。等价于 lhs.swap(rhs) 。
std::swap(tuple1, tuple2);
std::cout << "after swap: " << std::endl;
std::cout << "tuple1: ";
print(tuple1);
std::cout << "tuple2: ";
print(tuple2);
return 0;
}
输出
before swap:
tuple1: ({101,101}, GGX, 3.14)
tuple2: ({202,200}, XGG, 3.1415)
after swap:
tuple1: ({202,200}, XGG, 3.1415)
tuple2: ({101,101}, GGX, 3.14)
after swap:
tuple1: ({101,101}, GGX, 3.14)
tuple2: ({202,200}, XGG, 3.1415)
更多推荐
所有评论(0)