Tuple
A tuple is an object capable to hold a collection of elements. Each element can be of a different type.
template <class... Types> class tuple;
找到一个tuple模板:
template<typename Head, typename... Tail>
class Tuple<Head, Tail...>
:private Tuple<Tail...> // 私有继承自Tuple<Tail...>
{
using inherited = Tuple<Tail...>; //定义inherited 为 基类类型
public:
Tuple(){}
Tuple(Head v, Tail... vtail) // 将第一个元素 赋值给m_head, 其余传给基类
:m_head(v), inherited(vtail...)
{}
Head head() { return m_head; } // 返回元组的第一个元素
inherited& tail() { return *this; } // 返回该元组的基类引用
protected:
Head m_head;
记住在调用Tuple模板时,后面依次为父类。其他的细节不懂。哈哈哈哈 你打我啊。
Tuple 操作:
std::get<>()
auto mytuple = std::make_tuple (10,'a');
std::tuple_element<0,decltype(mytuple)>::type first = std::get<0>(mytuple);
std::tuple_element<1,decltype(mytuple)>::type second = std::get<1>(mytuple);
std::make_tuple
template<class... Types>
tuple<VTypes...> make_tuple (Types&&... args);
auto first = std::make_tuple (10,'a'); // tuple < int, char >
const int a = 0; int b[3]; // decayed types:
auto second = std::make_tuple (a,b); // tuple < int, int* >
auto third = std::make_tuple (std::ref(a),"abc"); // tuple < const int&, const char* >
std::tie(...) //建立一个由引用构成的tuple,并允许由此tuple提取个别数值。对应获取tuple里的值
int main ()
{
int myint;
char mychar;
std::tuple<int,float,char> mytuple;
mytuple = std::make_tuple (10, 2.6, 'a'); // packing values into tuple
std::tie (myint, std::ignore, mychar) = mytuple; // unpacking tuple into variables
std::cout << "myint contains: " << myint << '\n';
std::cout << "mychar contains: " << mychar << '\n';
return 0;
}
std::tuple_size<Tupletype>::value //类似于sizeof 需要的只是类型
std::tuple_cat(tuple1,tuple2)//拼接tuple
std::forward_as_tuple
template<class... Types>
tuple<Types&&...> forward_as_tuple (Types&&... args) noexcept
{
return tuple<Types&&...>(std::forward<Types>(args)...);
}
==//所有元素相等
!= //见==
swap(t1,t2)//呼唤数据。the same type
=//是一个危险的运算符,包含很多默认隐士转换。
tuple和初值列
显示调用多个参数的构造函数,避免隐士调用单个参数的构造函数发生错误。
template <typename ...types>
class tuple{
public:
explicit tuple(const types &...);
}
//std::tuple<int, double> t3 = {1, 2.3 }; 错误示例
// std::tuple<int, double> t1(1, 3.2);
// std::tuple<int, double> t2{ 1, 2.3 };最好使用make_tuple()
tuple标准输入输出
tuple 重载了<<
后面可能会补充一个paire 和 tuple 转换的函数