std::tuple<size_t, size_t, size_t> threeD; //使用默认构造函数
std::tuple<std::string, std::vector<double>, int, std::list<int>>someVal("constants", { 3.14,2.718 }, 42, { 0,1,2,3,4,5 });
string s;tuple<string&> t(s);get<0>(t) = "hello"; //s变为hello
template<typename... Args>void foo(const std::tuple<Args...> t);
int main(){foo(42); //错误,禁止隐式转换foo(make_tuple(42)); //正确return 0;}
std::tuple<size_t, size_t, size_t> threeD1 = { 1,2,3 }; //错误,不能进行隐式类型转换了std::tuple<size_t, size_t, size_t> threeD2{ 1,2,3 }; //正确
std::vector<std::tuple<int, float>> v{ { 1,1,0 },{2,2,0} }; //错误
std::tuple<int, int, int> foo() { //错误 return{ 1,2,3 };}
auto item = std::make_tuple("0-999-78345-X", 3, 20.00);
//item类型为tuple<const char*, int, double>
auto item = std::make_tuple("0-999-78345-X", 3, 20.00);
auto book = std::get<0>(item); //返回item第一个成员auto cnt = std::get<1>(item); //返回item第二个成员auto price = std::get<2>(item) / cnt; //返回最后一个成员,并将其除以cntstd::get<2>(item) *= 0.8; //打折20%(get返回的是item的引用)
std::tuple<int,double> t1;get<0>(t1); //正确
int i;get<i>(t1); //错误,运行期才传入一个索引值
get<3>(t1); //错误,索引错误,t1只有两个元素
//item的类型为tuple<const char*, int, double>auto item = std::make_tuple("0-999-78345-X", 3, 20.00);
//trans为item的数据类型typedef decltype(item) trans;
//返回item中的数据成员数量size_t sz = std::tuple_size<trans>::value;
//type为intstd::tuple_element<1, trans>::type cnt = get<1>(item);
int n;
//tt的类型为tuple<int,double,std::string,int>auto tt = std::tuple_cat(std::make_tuple(42, 7.7, "hello"), std::tie(n));
std::tuple<std::string, std::string> duo("1", "2");
std::tuple<size_t, size_t> twoD(1, 2);
bool b = (duo == twoD); //错误,string与size_t类型不一致无法比较
std::tuple<size_t, size_t, size_t> threeD(1, 2, 3);
b = (twoD < threeD); //错误,tuple的成员数量不同
std::tuple<size_t, size_t> origin(0, 0);
b = (origin > twoD); //正确,b为true
//三个元素的类型是int、int、const char*make_tuple(22, 44, "nico");
//其实第三个元素的类型为const char[5]。当使用type trait std::decay()时,第三个元素的类型衰退为const char*
std::string s;
auto x = std::make_tuple(s); //以s的非引用创建一个tuplestd::get<0>(x) = "my value"; //未改变s的值
auto y = make_tuple(ref(s)); //以s的引用创建一个tuplestd::get<0>(y) = "my value"; //改变s的值
std::tuple<int, float, std::string> t(77, 1.1, "more light");
int i;float f;std::string s;std::tie(i, f, s) = t;//将t赋值给一个tuple对象(tie()创建返回的tuple),其中都使用i,f,s的引用来创建//因此,创建之后,i=77,f=1.1,s="more light"
std::tuple<int, float, std::string> t(77, 1.1, "more light");
int i;std::string s;//使用t创建一个tuple,tuple的类型为tuple<int,std::string>,其中忽略了t的第二个元素std::tie(i, std::ignore, s) = t;
#include <iostream>
#include <ostream>
#include <sstream>
#include <fstream>
#include <string>
#include <iterator>
#include <vector>
#include <tuple>
#include <algorithm>
#include <numeric>
using namespace std;
struct Sales_data {
std::string isbn()const { return bookNo; }
Sales_data(const string& s) :bookNo(s), units_sold(0), revenue(0) {}
Sales_data &operator+=(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
ostream &operator<<(ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << ;
return os;
}
Sales_data operator+(const Sales_data &lhs, const Sales_data &rhs) {
Sales_data sum = lhs;
sum += rhs;
return sum;
}
/*vector<Sales_data>代表一家书店的销售情况vector<vector>代表所有书店*/vector<vector<Sales_data>> files;
//equal_range算法比较函数
bool compareIsbn(const Sales_data& lhs, const Sales_data& rhs)
{
return lhs.isbn() < rhs.isbn();
}
vector<vector<Sales_data>> files;
//声明一个别名
typedef tuple<vector<Sales_data>::size_type,
vector<Sales_data>::const_iterator,
vector<Sales_data>::const_iterator> matches;
//返回一个类型为tuple的vector
vector<matches> findBook(const vector<vector<Sales_data>> &files, const string &book)
{
vector<matches> ret;
//遍历files
for (auto it = files.cbegin(); it != files.cend(); ++it)
{
//在it所在的书店中寻找名为book的书籍,返回值见上面介绍
auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn);
//如果找到了,添加到ret中
if (found.first != found.second)
ret.push_back(std::make_tuple(it - files.cbegin(), found.first, found.second));
}
return ret;
}
void reportResults(istream &in, ostream &os, const vector<vector<Sales_data>> &files)
{
std::string s; //要查找的书籍
while (in >> s)
{
auto trans = findBook(files, s);
if (trans.empty()) {
std::cout << s << "not found in any stores" << std::endl;
continue;
}
else {
for (const auto &store : trans) {
os << "store: " << std::get<0>(store) << std::endl;
os << "sales: " << std::accumulate(std::get<1>(store), std::get<2>(store), Sales_data(s)) << std::endl;
}
}
}
}
演示案例:
#include "printtuple.hpp"
#include <tuple>
#include <iostream>
#include <string>
using namespace std;
int main()
{
tuple <int,float ,string> t(77,1.1, "more light");
cout<< "io: " <<t << endl;
}