Class Pair和Class Tuple
// demo5.1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <string> #include <tuple> typedef std::pair<int, double> InfoPair; typedef std::tuple<int, std::string, double> InfoTuple; std::ostream& operator <<(std::ostream& os, InfoPair& p) //重载<<运算符 { return os << "[" <<p.first << "," << p.second << "]" << std::endl; } int _tmain(int argc, _TCHAR* argv[]) { InfoPair p(3,4); InfoTuple t(5,"gg",4.4); std::cout << p.first << std::endl; //直接访问元素的值 std::cout << std::get<0>(p) << std::endl; std::cout << std::get<0>(t) << std::endl; std::cout << std::get<1>(t) << std::endl; std::cout << std::get<2>(t) << std::endl; std::cout << std::tuple_size<InfoPair>::value << std::endl; //元素的个数 std::cout << std::tuple_size<InfoTuple>::value << std::endl; //元素的个数 std::cout << (std::tuple_element<0, InfoPair>::type)5.5 << std::endl;//元素的类型 std::cout << (std::tuple_element<2, InfoTuple>::type)5.5 << std::endl; std::cout << p << std::endl; //重载<< std::pair<char, char> p1 = std::make_pair (‘x‘, ‘y‘); char c; std::tie(std::ignore, c) = p1; std::cout << c << std::endl; //tie接口 auto t1 = std::make_tuple(12,"1e2",1); std::string s; std::tuple<std::string&> t2(s); std::get<0>(t2) = "guojun"; std::cout << s << std::endl; //tuple元素类型可以引用 std::string s1; int i; std::pair<std::string,int&> t3(s1,i); //pair元素类型可以引用 std::get<0>(t3) = "guojun"; std::get<1>(t3) = 1000; std::cout << s1 << std::endl; std::cout << i << std::endl; // auto t4 = std::make_tuple(ref(s)); // auto t5 = std::make_tuple(cref(s)); auto t6 = std::make_tuple(s); std::get<0>(t6) = "hehhe"; std::cout << s << std::endl; std::tie(std::ignore,s,i) = t1; //tie接口 std::cout << s << std::endl; std::cout << i << std::endl; int n = 99; std::tie(n) = std::make_tuple(0); std::cout << n << std::endl; int n1 = 100; auto tt = std::tuple_cat(std::make_tuple(1, 1, "de", 1), std::tie(n1)); std::cout << std::get<0>(tt) << std::endl; std::cout << std::get<1>(tt) << std::endl; std::cout << std::get<2>(tt) << std::endl; std::cout << std::get<3>(tt) << std::endl; std::cout << std::get<4>(tt) << std::endl; system("pause"); return 0; }
时间: 2024-10-17 03:34:28