现在的位置: 首页 > 综合 > 正文

c++ 元组使用

2013年11月30日 ⁄ 综合 ⁄ 共 617字 ⁄ 字号 评论关闭

//元组使用:
Poco::Tuple<int,string,bool>a(5,"333",false);
 
cout<<a.get<0>()<<endl;
cout<<a.get<1>()<<endl;
cout<<a.get<2>()<<endl;
 
a.set<0>(10);
a.set<1>("444");
a.set<2>(true);
 
cout<<a.get<0>()<<endl;
cout<<a.get<1>()<<endl;
cout<<a.get<2>()<<endl;

元组应用:

例如你有这样一个函数void
test(
std::string, int, bool,float, char, long, double, short, std::string, int);

n个参数,这样这个函数显得参数很多,

有两个办法可以解决它,

1 定义一个结构体

2 (据说就是因为程序员懒,所以他们写了很多程序来代替这些工作),嫌定义个结构体麻烦,你可用元组,

 

先定义个元组: (Pocoboost中都有随你)

typedef     Tuple<std::string, int, bool, float, char, long, double, short, std::string,int> TupleType;

 

你的函数可以这样定义 voidtest(TupleType mydata);

 

这样一来妈妈再也不用为我写函数参数多又不想写结构体而担心了

缺点 :代码可能读性不好
0 1 2
谁知道什么意思

抱歉!评论已关闭.