如果您关心命名问题(即,事实上,用户定义的结构体 struct 对其 每个成员都有一个有意义的名称 ,而元组却没有),那么您就要求元组实现一些它不应该实现的目标。
简单地说,如果您想要返回的是具有高级语义的数据包,并且您想要使用的包结构将来可能会被重用,那么您可能应该使用结构,甚至是成熟的类。
然而,Tuple并不是为这种情况而设计的;相反,应用元组的情况是,您所需要的只是即时获得的轻量级打包机制,并且在使用之后可能会丢弃它。
#include <tuple>
#include <functional>
#include <utility>
#include <typeinfo>
void Person::doTupleTest()
{
//1.构造tuple
std::tuple<int, std::string, double> a1(1, "rock", 99.5);
std::tuple<int, std::string, double> a2(std::make_tuple(2, "bob", 95.5));
//2.读取tuple中的值
std::cout << std::get<0>(a1) << ‘\t‘ << std::get<1>(a1).c_str() << ‘\t‘ << std::get<2>(a1) << std::endl;
//3.修改tuple中的值
std::get<2>(a1) = 100;
std::cout << std::get<0>(a1) << ‘\t‘ << std::get<1>(a1).c_str() << ‘\t‘ << std::get<2>(a1) << std::endl;
//4.搭配 typedef
typedef std::tuple<int, std::string, double> MyTuple;
MyTuple a3(3, "lily", 100);
std::cout << std::get<0>(a3) << ‘\t‘ << std::get<1>(a3).c_str() << ‘\t‘ << std::get<2>(a3) << std::endl;
std::get<2>(a3) = 101.0;
std::cout << std::get<0>(a3) << ‘\t‘ << std::get<1>(a3).c_str() << ‘\t‘ << std::get<2>(a3) << std::endl;
//5.获取 tuple 中的元素个数
auto a3Size = std::tuple_size<decltype(a3)>::value;
std::cout << "the element size of a3 is " << a3Size << std::endl;
//6.结合 tie ,对 tuple 进行解包为独立对象 (不需要的元素可以 ignor)
int a1No = 0;
double a1Score = 0.0;
std::tie(a1No, std::ignore, a1Score) = a1;
std::cout << "a1No:" << a1No << "\t\ta1Score:" << a1Score << std::endl;
}
得到
//1.解包tuple或批量赋值
std::tuple<int, std::string, double> b1(11, "rockman", 99.5);
int n;
std::string s;
double d;
std::tie(n, s, d) = b1;
std::cout << "n:" << n << "\t\ts:" << s.c_str() << "\t\td:" << d <<std::endl;
//2.解包pair
std::map<int, std::string> m;
std::map<int, std::string>::iterator it;
bool isInserted;
std::tie(it, isInserted) = m.insert(std::make_pair(1, "rock"));
if (isInserted)
{
std::cout << "Value was inserted successfully!" << std::endl;
}
//3.搭配ignor只提取关注的值
std::tie(std::ignore, s, std::ignore) = b1;
std::cout << "s: " << s.c_str() << std::endl;
//tie搭配ignor判断pair返回值非常方便
std::tie(std::ignore, isInserted) = m.insert(std::make_pair(2, "bob"));
if (isInserted)
{
std::cout << "Value was inserted successfully!" << endl;
}
https://blog.csdn.net/fengbingchun/article/details/72835446
https://www.jb51.net/article/191040.htm
https://www.jb51.net/article/196971.htm
https://blog.csdn.net/janeqi1987/article/details/102500340
原文:https://www.cnblogs.com/rock-cc/p/14666816.html