0
点赞
收藏
分享

微信扫一扫

std::pair和std::tuple

纽二 2024-09-25 阅读 27
开发语言

文章目录

前言

前期疑问:
本文目标:


一、背景

最近

std::pair和std::tuple

二、用法

1.1 创建

看代码规范,提到:通过std::pair 和std::tuple ,函数可以同时返回多个值。相比起使用额外参数来获取输出,返回值能让阅读者更明确函数
输出的数据

查了下std::pair和std::tuple的用法,后面的代码中可以尝试使用

[C++] std::tuple

下面的是之前看过并记录的文档

2024年9月10日19:15:37

下面是自己写的验证代码

std::tuple<int, float, bool> tupleFunc()
{
    int a = 1;
    float b = 1.2f;
    bool c = true;
    std::tuple<int, float, bool> ret = std::make_tuple(a, b, c);

    return ret;
}

int main()
{
    std::tuple<int, float, bool> retTuple = tupleFunc();
    int a = std::get<0>(retTuple);
    float b = std::get<1>(retTuple);
    bool c = std::get<2>(retTuple);
    std::cout << "a:    " << a << " b:    " << b << " c:   " << c << std::endl;

    return 0;
}
// 打印信息 
// a:    1 b:    1.2 c:   1

先写一下能满足自己使用的验证代码,可能有其他复杂功能,不过可以等到需要用的时候再深究。

1.2、std::tuple声明初始化为0

#include <tuple>
#include <iostream>
 
int main() {
    // 使用 std::make_tuple 创建一个含有0的 std::tuple
    auto my_tuple = std::make_tuple(0, 0, 0);
 
    // 访问并打印 tuple 中的元素
    std::cout << "Tuple element 0: " << std::get<0>(my_tuple) << std::endl;
    std::cout << "Tuple element 1: " << std::get<1>(my_tuple) << std::endl;
    std::cout << "Tuple element 2: " << std::get<2>(my_tuple) << std::endl;
 
    return 0;
}

三、

3.1


总结

未完待续

举报

相关推荐

0 条评论