关于 C++ STL 中 tuple 使用的简单介绍。

# 概述

tuple (元组) 是 C++ STL 中的一种容器,可以看作一种结构体。使用时需要引入头文件 <tuple> . 此外, <tuple> 中还封装了 pair , 作为只有两个元素的元组使用。

# 声明

tuple<int, int, int> t1;
t1 = make_tuple(24,4,2023);
tuple<int, int, int> t2(25,4,2023);
auto t3 = make_tuple(1,5,2023);

# 基本函数

# 成员函数

# 非成员函数

get<n>(t) 可以获取元组 t 中第 n 项的值 (从 0 计数)。此外, get 还可以用来获取某一类型的数据的值,但该类型的数据在元组中只能有一项。下面的代码解释了这一点:

t1 = make_tuple(1,1,"this");
int n1 = get<0>(t1);
// int n2 = get<int>(t1); -- Compiling Error
string s = get<string>(t1);

# Reference

  • CSDN: C++ STL 之 tuple 详解
  • cpp_reference: std::tuple