<tuple>是C++11引入的,它是一个可以存放多个元素的“容器”(并不是STL容器),其中每个元素的类型都可以不相同。tuple是一个固定大小的不同类型值的集合,是泛化的std::pair。
Tuple的使用
make_tuple()和get()方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> #include <string> // 元组的头文件 #include <tuple> using namespace std; int main(int argc, char* argv[]) { // 声明元组 tuple <string, int, float> abc; // 初始化元组 abc = make_tuple("snail", 100, 11.1); // 打印元组的值 cout << get<0>(abc) << " " << get<1>(abc) << " " << get<2>(abc) << endl; // 更改元组的值 get<0>(abc) = "pi"; get<2>(abc) = 3.14; // 打印元组的值 cout << get<0>(abc) << " " << get<1>(abc) << " " << get<2>(abc) << endl; return 0; } |
由于使用了C++11标准,注意在编译时添加编译选项 -std=c++11;编译运行:
1 2 3 4 |
$ g++ test.cpp -std=c++11 $ ./a.out snail 100 11.1 pi 100 3.14 |
tuple_size()方法:返回元组中元素个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> #include <tuple> using namespace std; int main(int argc, char* argv[]) { tuple <int, string, float> tup(100,"hello",34.33); cout << tuple_size<decltype(tup)>::value << endl; return 0; } |
1 2 3 |
$ g++ test.cpp -std=c++11 $ ./a.out 3 |
swap()方法:相互交换两个元组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <string> #include <tuple> using namespace std; int main(int argc, char* argv[]) { tuple <int, string, float> tup1(100,"hello",34.33); tuple <int, string, float> tup2(1,"world", 555.5); cout << "交换之前的元组" << endl; cout << get<0>(tup1) << " " << get<1>(tup1) << " " << get<2>(tup1) << endl; cout << get<0>(tup2) << " " << get<1>(tup2) << " " << get<2>(tup2) << endl; tup1.swap(tup2); cout << "交换之后的元组" << endl; cout << get<0>(tup1) << " " << get<1>(tup1) << " " << get<2>(tup1) << endl; cout << get<0>(tup2) << " " << get<1>(tup2) << " " << get<2>(tup2) << endl; return 0; } |
1 2 3 4 5 6 7 8 |
$ g++ test.cpp -std=c++11 $ ./a.out 交换之前的元组 100 hello 34.33 1 world 555.5 交换之后的元组 1 world 555.5 100 hello 34.33 |
tie()方法:把元素解包成单独的值,相当于Python中元组的解包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <string> #include <tuple> using namespace std; int main(int argc, char* argv[]) { tuple <int, string, float> tup(100,"hello",34.33); int num1; string str; float num2; tie(num1, str, num2) = tup; cout << num1 << " " << str << " " << num2 << endl; // 使用ignore参数不解包某个元素 tie(num1, ignore, num2) = tup; cout << num1 << " " << num2 << endl; return 0; } |
1 2 3 4 |
$ g++ test.cpp -std=c++11 $ ./a.out 100 hello 34.33 100 34.33 |
tuple_cat()方法:连接两个元祖
1 2 3 4 5 |
tuple <int, string, float> tup1(100,"hello",34.33); tuple <int, string, float> tup2(1,"world", 555.5); auto tup3 = tuple_cat(tup1,tup2); // tup3 (100,"hello",34.33, 1,"world", 555.5) |
Tuple的排序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <string> #include <vector> #include <tuple> #include <algorithm> using namespace std; typedef tuple<string,double,int> mytuple; bool mycompare (const mytuple &lhs, const mytuple &rhs){ return get<1>(lhs) < get<1>(rhs); } int main(void) { vector<mytuple> data; data.push_back(make_tuple("abc",4.5,1)); data.push_back(make_tuple("def",5.5,-1)); data.push_back(make_tuple("wolf",-3.47,1)); sort(data.begin(),data.end(),mycompare); for(vector<mytuple>::iterator iter = data.begin(); iter != data.end(); iter++) { cout << get<0>(*iter) << "\t" << get<1>(*iter) << "\t" << get<2>(*iter) << endl; } } |