#include<bits/stdc++.h>
using namespace std;
int main(){
tuple<size_t,string> p1;
tuple<size_t,string> p2{1,"gao"};
p1=make_tuple(1,"gao");
cout<<get<0>(p1)<<endl;
cout<<get<1>(p1)<<endl;
p2=p1;
if(p2==p1)cout<<"yes"<<endl;
if(p2<p1)cout<<"xiaui"<<endl;
//查询一个tuple具体的细节信息。
//查询一个tuple的数量
typedef decltype(p1) trans;
size_t sz = tuple_size<trans>::value;
cout<<sz<<endl;
tuple_element<1,trans>::type cnt = get<1>(p1);//获取第二个数据的类型,并且赋值给cnt。
cout<<cnt<<endl;
}
tuple一个常见用途是从一个函数返回多个值。
#include<bits/stdc++.h>
using namespace std;
//每个书店的账簿都是一个vector<Node>,所有书店的是vector<vector<Node>>
struct Node{string bool;};
typedef tuple< vector<Node>::size_type, vector<Node>::const_iterator, vector<Node>::const_iterator> matches;
vector<matches> find_book(vector<vector<Node> > &files,const string &book){//找到某本书的信息
vector<matches> ret;
for(auto it=files.cbegin(),it!=files.cend();it++){
auto found = equal_range(it->cbegin(),it->cend(),book,cmp);
if(found.first!=found.second)
ret.push_back(make_tuple(it-files.cbegin(),found.first,found.second));
}
return ret;
}//ret中每个元素是数量,起始指针,结尾指针
//使用函数返回的tuple
void reportResults(itsream &in,ostream &os,const vector<vector<Node> >&files){
string s;
while(in>>s){
auto trans = find_book(files,s);
if(trans.empty()){
os<<"没有找到这个值"<<endl;
continue;
}
for(const auto &store:trans){//遍历tuple
os<< "store" << get<0>store << accumulate(get<1>store,get<2>(store),Node(s))<<endl;//假设Node定义了加法运算
}
}
}
int main(){
return 0;
}