C/C++——tuple小记

基本使用

cpp 复制代码
#include<tuple>
using namespace std;

int main() 
{
    tuple<int, string, int> t(1,string("apple"),2);

    //这样创建避免写类型
    auto t1 = make_tuple(2,5,'a');

    cout << get<0>(t) << endl;
    cout << get<1>(t) << endl;
    cout << get<2>(t) << endl;

    cout << get<0>(t1) << endl;
    cout << get<1>(t1) << endl;
    cout << get<2>(t1) << endl;

    //tie用于提取tuple中的属性
    int x, y; string s;
    tie(x, s, y) = t;
    cout << x << endl;
    cout << s << endl;
    cout << y << endl;

    //更加简单的属性提取方式
    auto [a, b, c] = t;
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
}

手搓tuple

cpp 复制代码
//tuple的原版本
template<class...Args>
class tuple 
{
public:
};

//这是tuple的特化版本,表示至少有一个非空模版参数
template<class T,class...Args>
class tuple<T,Args...> : private tuple<Args...>
{
private:
    T _value;
public:
    tuple(T x,Args... args)
	:tuple<Args...>(args...),
	_value(x)
    {}

    template<size_t n>
    auto& get() 
    {
	if constexpr (n == 0) { return _value; }
	else 
	{
	    return tuple<Args...>::template get<n-1>();
	}
    }
};

//利用递归模版实例化+类继承机制来让tuple类有多个父类,每个父类都存放tuple中给的一个属性
//get函数就是递归调用父类的get


int main()
{
    tuple<int,char,int> t(1,'x',2);
    std::cout << t.get<0>() << std::endl;
    std::cout << t.get<1>() << std::endl;
    std::cout << t.get<2>() << std::endl;
}

**我们的get函数是使用编译时期递归模版实例化来实现,由于tuple的不同值的类型可能是不同的,就会造成这样的困境:if分支返回int,else分支推导出来其实是返回char,那么auto究竟是什么类型。**constexpr在这里的作用非常关键,当一个分支被constexpr修饰,那么只要编译时期确定了n不为0,这个分支就不会被编译,从而解决了返回值类型歧义。

相关推荐
神仙别闹12 分钟前
基于C++实现的(控制台)学校开课查询系统
开发语言·c++
Dylan的码园30 分钟前
A+B问题 进制处理【逻辑控制 运算符】
c++·算法
hetao17338371 小时前
2026-09-27~28 hetao1733837 的刷题记录
c++·算法
2601_962218611 小时前
C++的几种编译器的实现
开发语言·c++
多弗朗皮卡丘1 小时前
C++ stack和queue
开发语言·c++·deque·queue·stack·容器适配器
lengxuenong1 小时前
枚举及其优化学习手册
c++·学习·算法·青少年编程
Benny_Tang2 小时前
P9527 [JOIST 2022] 洒水器 / Sprinkler 题解
c++·算法
垆边人似月.2 小时前
华为机考题:最小代价爬楼梯
c语言·c++
可涵不会debug2 小时前
C++ std::bind 实战教程:线程池、回调场景全覆盖
linux·开发语言·c++
垆边人似月.3 小时前
华为机考题:二叉树的最大路径和
c语言·c++