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,这个分支就不会被编译,从而解决了返回值类型歧义。

相关推荐
丰色木夕1 小时前
Ubuntu22.04下源码编译安装USD-Brep
c++·ubuntu·nvidia·usd-brep
衍玊4 小时前
多路转接原理与应用
c++
himobrinehacken6 小时前
位运算揭秘:左右移与符号位陷阱
c语言·c++·算法·安全
Android小码家8 小时前
Vehicle 实战 自定义属性+测试demo
java·c++·framework·vhal·vehicle
hetao17338378 小时前
2026-10-19 hetao1733837 的刷题记录
c++·算法
夏虫不可与之言冰8 小时前
第三课 点亮 LED 与定时器非阻塞闪烁
c语言·c++·vscode·单片机·嵌入式硬件·物联网
辛苦才能9 小时前
C++ 哈希表深度解析:删除为什么必须打标记,负载因子为什么卡 0.7
开发语言·数据结构·c++·算法
miller-tsunami9 小时前
STL简介&string相关知识点
开发语言·c++·string
坤坤子吖9 小时前
Linux 线程同步与互斥——互斥锁
linux·服务器·c++·笔记·学习
TDTC_XQ9 小时前
Linux网络编程accept的讲解
linux·服务器·c++