主页 > 互联网  > 

C++中tuple数据结构使用

C++中tuple数据结构使用

目录 1 基础知识2 模板3 工程化

1 基础知识

tuple是元组,一旦定义则不可修改,它可以存储一组不同类型的数据。它定义在头文件#include <tuple>中。

(一) tuple变量的定义。

tuple<int, float, string> a = {1, 2.0, "three"}; tuple<int, float, string> b(1, 2.0, "three"); tuple<int, float, string> c = make_tuple(1, 2.0, "three");

(二) tuple变量的访问。

int a1 = get<0>(a); float a2 = get<1>(a); string a3 = get<2>(a); int b1; float b2; string b3; tie(b1, b2, b3) = b; 2 模板 #include <iostream> #include <tuple> #include <string> using namespace std; int main() { tuple<int, float, string> a = {1, 2.0, "three"}; tuple<int, float, string> b(1, 2.0, "three"); tuple<int, float, string> c = make_tuple(1, 2.0, "three"); int a1 = get<0>(a); float a2 = get<1>(a); string a3 = get<2>(a); cout << "a1 = " << a1 << ", a2 = " << a2 << ", a3 = " << a3 << endl; int b1; float b2; string b3; tie(b1, b2, b3) = b; cout << "b1 = " << b1 << ", b2 = " << b2 << ", b3 = " << b3 << endl; return 0; }

上述代码输出,

a1 = 1, a2 = 2, a3 = three b1 = 1, b2 = 2, b3 = three 3 工程化

暂无。。。

标签:

C++中tuple数据结构使用由讯客互联互联网栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“C++中tuple数据结构使用