一 简介
头文件 <tuple>
template< class... Types >
class tuple; (C++11)
二 取值及辅助函数
#include <functional> // tie
#include <iostream> // cout cin
#include <tuple> // tuple
int main() {
{
// 取值
std::tuple<int, double> t{1, 1.1};
std::cout << "std::get<0>(t): " << std::get<0>(t) << std::endl;
std::cout << "std::get<1>(t): " << std::get<1>(t) << std::endl;
}
{
// 辅助函数
// tie ignore
int i = 0;
double d = 0;
std::tuple<int, double> t{1, 2.2};
std::tie(i, d) = t;
std::tie(std::ignore, i) = t;
std::cout << "i: " << i << " d: " << d << std::endl;
// make_tuple ref
auto m = std::make_tuple(3, 4);
int i1 = 0;
double d1 = 0;
auto m1 = std::make_tuple(std::ref(i1), d1);
std::get<1>(m1) = 100; // i1 = 100
// tuple_element
using Type = std::tuple_element<0, decltype(m)>::type; // int
Type e = 100;
// tuple_cat
auto c = std::tuple_cat(t, m);
// tuple_size
std::cout << "std::tuple_size<decltype(c)>::value: "
<< std::tuple_size<decltype(c)>::value << std::endl;
}
std::cin.get();
}
三 tuple打印
template <int Index, int Size, typename ...Args>
class PrintTuple{
public:
static void print(std::ostream& os, const std::tuple<Args...>& t) {
os << std::get<Index>(t) << ((Index + 1 == Size) ? " " : ", ");
PrintTuple<Index + 1, Size, Args...>::print(os, t);
}
};
template <int Size, typename... Args>
class PrintTuple<Size, Size, Args...> {
public:
static void print(std::ostream& os, const std::tuple<Args...>& t) {
}
};
template <typename ...Args>
std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& t) {
os << "[";
PrintTuple<0, sizeof...(Args), Args...>::print(os, t);
os << "]";
return os;
}
// main
auto m = std::make_tuple(1, 2, "hello");
std::cout << "m: " << m;
两段代码结果:

四 参考
本文深入探讨了C++中tuple类的使用方法,包括如何创建、取值、使用辅助函数进行操作,以及如何自定义tuple的打印方式。通过实例展示了std::get、std::tie、std::make_tuple等函数的用法,并介绍了tuple_element、tuple_cat和tuple_size等模板元编程特性。
559

被折叠的 条评论
为什么被折叠?



