当前位置:首页 > 编程笔记 > 正文
已解决

C++中tuple数据结构使用

来自网友在路上 175875提问 提问时间:2023-11-19 14:07:13阅读次数: 75

最佳答案 问答题库758位专家为你答疑解惑

目录

  • 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 工程化

暂无。。。

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"C++中tuple数据结构使用":http://eshow365.cn/6-39342-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!