tuple(元组):定义了一个有固定数目元素的容器,其中的每个元素类型都可以不相同,这与其他容器有着本质的区别。它是std::pair的泛化,可以从函数返回任意数值的值。

tuple默认最多支持10个模板类型参数,也就是说它最多能容纳10个不同类型的元素。

tie()可以方便地利用现有普通变量创建一个可赋值的tuple对象,因此可以对tuple执行"解包"操作.

#include<boost/tuple/tuple.hpp>
#include<string>
#include<iostream>
using namespace boost;
using namespace std;

int main()
{
tuple<int,string,char> t(1,"abc",'d');
t = make_tuple(2,"def",'g');
cout << t.get<0>()<<endl;
for(int i=0;i<3;++i){
//这是错误的,get<>()是一个模板函数,编译进行模板实例化时要求模板参数
//<int N>必须是编译期可确定的,而for循环中的变量i只能在运行时确定,因而
//无法编译代码
//cout << t.get<i>() << endl;// error: ‘i’ cannot appear in a constant-expression
}
int i;
string s;
char c;
tie(i,s,c) = t;
cout <<"i,s,c:"<<i<<","<<s<<","<<c<<endl;

tie(i,s) = make_tuple(3,"aaa");//make_tuple
cout <<"std::pair:i,s"<<i<<","<<s<<endl;
//tuples::ignore相当于占位符,可以忽略某些对象,如下只接收string值
tie(tuples::ignore,s) = make_pair(4,"bbb");//make_pair
cout << s << endl;
}


2
i,s,c:2,def,g
std::pair:i,s3,aaa