赞
踩
std::tuple
是C++11新标准引入的一个类模板,又称元组,是一个固定大小的异构值集合,由std::pair
泛化而来。pair
可以看作是tuple
的一种特殊情况,成员类目限定为两个。tuple
可以有任意个成员数量,但是每个确定的tuple
类型的成员数目是固定的。
从概念上讲,它们类似于C的结构体,但是不具有命名的数据成员,我们也可以把他当作一个通用的结构体来用,不需要创建结构体又获取结构体的特征,在某些情况下取代结构体使程序更简洁,直观。
tuple
本质上是一个以可变模板参数定义的类模板,它定义在头文件并位于std
命名空间中。因此要使用tuple类模板,程序中需要引入一下代码:
#include <tuple>
实例化typle
模板类对象常用的方法有两种,一种是借助该类的构造函数,令一中是借助make_typle()
函数
constexpr typle(); //无参构造函数 tuple(const Type&... args); //拷贝构造函数 typle(typle&& tpl); //移动构造函数 // 隐式类型转换构造函数 template<class ... UTypes> tuple(const typle<UTypes...>& other); //左值方式 template<class ... UTypes> tuple(typle<UTypes...>& other); //左值方式 //支持初始化列表的构造函数 explicit tuple (const Types&... elems); //左值方式 template <class... UTypes> explicit tuple (UTypes&&... elems); //右值方式 //将pair对象转换为tuple对象 template< class U1, class U2 > tuple( const std::pair<U1, U2>& p ); template< class U1, class U2 > tuple( std::pair<U1, U2>&& p );
Example
#include <iostream>
#include <tuple>
int main()
{
std::tuple<std::string, int> t1; //无参构造
std::tuple<std::string, int> t2(t1); //拷贝构造
std::tuple<std::string, int> t3(std::make_tuple("haha", 1));
std::tuple<std::string, long> t4(t3); //隐式类型转换构造的左值方式
std::tuple<std::string, int> t5("Mike", 2); //初始化列表构造的右值方式
std::tuple<std::string, int > t6(std::make_pair("Jack", 3)); //将pair对象转换为tuple对象
}
make_tuple()函数以模板的形式定义在头文件中,功能是创建一个tuple右值对象(或者临时对象)
Exmaple
std::tuple<std::string, double, int> t1 = std::make_tuple("Jack", 66.6, 99);
auto t2 = std::make_tuple(1, "Lily", 'c');
Example
#include <iostream>
#include <tuple>
int main()
{
auto t1 = std::make_tuple(1, "PI", 3.14);
std::cout << '(' << std::get<0>(t1) << ','
<< std::get<1>(t1) << ','
<< std::get<2>(t1) <<')' << std::endl;
return 0;
}
// output:(1,PI,3.14)
值得注意的是,由于get的特性,tuple不支持迭代,只能同构元素索引(或tie解包)进行获取元素的值。但是给定的索引必须是在编译器就已经给定,不能在运行期进行动态传递,否则将发生编译错误
解包时,我们如果只想解某个位置的值时,可以用std::ignore占位符来表示某个位置的值
Example
#include <iostream> #include <tuple> int main() { auto t1 = std::make_tuple(1, "PI", 3.14); auto t2 = std::make_tuple(2, "MAX", 999); std::cout << '(' << std::get<0>(t1) << ',' << std::get<1>(t1) << ',' << std::get<2>(t1) <<')' << std::endl; int num = 0; std::string name; double value; std::tie(num, name, value) = t1; std::cout << '(' << num << ',' << name << ',' << value << ')' << std::endl; num = 0, name = "", value = 0; std::tie(std::ignore, name, value) = t2; std::cout << '(' << num << ',' << name << ',' << value << ')' << std::endl; return 0; } /* (1,PI,3.14) (1,PI,3.14) (0,MAX,999) */
std::tuple_size<T>::value
来获取,其中T必须要显示给出tuple的类型;Example:
#include <iostream>
#include <tuple>
int main()
{
auto t1 = std::make_tuple(2, "MAX", 99, 88 , 65.6);
std::cout << "The t1 has elements:" << std::tuple_size<decltype(t1)>::value << std:: endl;
return 0;
}
/*
output: The t1 has elements: 5
*/
可以直接采用std::tuple<size_t i,decltype(tuple)>::type
来获取;
#include <iostream> #include <tuple> int main() { auto t1 = std::make_tuple(2, "MAX", 999.9); std::cout << "The t1 has elements: " << std::tuple_size<decltype(t1)>::value << std::endl; std::tuple_element<0, decltype(t1)>::type type0; std::tuple_element<0, decltype(t2)>::type type0; std::tuple_element<0, decltype(t3)>::type type0; type0 = std::get<0>(t1); type1 = std::get<1>(t1); type2 = std::get<2>(t2); std::cout << "type0 : " << type0 << std::endl; std::cout << "type1 : " << type1 << std::endl; std::cout << "type2 : " << type2 << std::endl; return 0; } /* output: The t1 has elements: 3 type0 : 2 type1 : MAX type2 : 999.9 */
std::tuple<T1&> t3(ref&); //tuple的元素类型可以是一个引用
引用声明代表对于创建tuple的对应元素而言,改变tuple的值会影响原有变量
Example:
#include <iostream> #include <tuple> #include <fuctional> int main() { auto t1 = std::make_tuple("test", 85.8, 185); std::string name; double weight; int height; auto t2 = std::make_tuple(std::ref(name), std::ref(wegith), std::ref(height)) = t1; std::cout << "Before change :" << endl; std::cout << '(' << name << ' ' << weight << ' ' << height << ')' << std::endl; name = "spark", weight = 95.6, height = 188; std::cout << "After change :" << std::endl; std::cout << '(' << std::get<0>(t2) << ' ' << std::get<1>(t2) << ' ' << std::get<2>(t2) << ')' << '\n'; return 0; } /* output: Before change : (test 85.8 185) After change : (spark 95.6 188) */
参考:https://blog.csdn.net/weixin_55664293/article/details/125365041
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。