tuple总结

1.tuple

1.1添加数据:
std::make_tuple 创建 tuple
std::make_tuple(dv, 2 * MAX);
1.2访问数据:

std::tuple<int, double, std::string> myTuple(10, 3.14, "Hello");
int intValue = std::get<0>(myTuple);
double doubleValue = std::get<1>(myTuple);
std::string stringValue = std::get<2>(myTuple);

2.1、tuple的创建和初始化

(1)直接初始化:

#include <tuple>
#include <string>

int main() {
   
    // 直接初始化一个tuple
    std::tuple<int, double, std::string> myTuple(10, 3.14, "Hello");
    
    return 0;
}

(2)使用std::make_tuple函数进行初始化:

#include <tuple>
#include <string>

int main() {
   
    // 使用make_tuple函数创建一个tuple
    auto myTuple = std::make_tuple(10, 3.14, "Hello");
    
    return 0;
}

(3)使用std::tie进行结构化绑定的初始化:

#include <tuple>
#include <string>

int main() {
   
    int a;
    double b;
    std::string c;

    std::tie(a, b, c) = std::make_tuple(10, 3.14, "Hello");
    
    return 0;
}

(4)使用std::forward_as_tuple进行创建和初始化:

#include <tuple>
#include <string>

int main() {
   
    auto myTuple = std::forward_as_tuple(10, 3.14, "Hello");

    return 0;
}

2.2、tuple的成员访问
(1)使用 std::get 函数按索引访问元素:

#include <iostream>
#include <tuple>

int main() {
   
    std::tuple<int, double, std::string> myTuple(10, 3.14, "Hello");
    
    int intValue = std::get<0>(myTuple);
    double doubleValue = std::get<1>(myTuple);
    std::string stringValue = std::get<2>(myTuple);
    
    std::cout << "Int value: " << intValue << std::endl;
    std::cout << "Double value: " << doubleValue << std::endl;
    std::cout << "String value: " << stringValue << std::endl;
    
    return 0;
}

(2)使用 std::tie 进行结构化绑定,将 tuple 成员绑定到指定的变量:

#include <iostream>
#include <tuple>

int main() {
   
    int a;
    double b;
    std::string c;
    std::tuple<int, double, std::string> myTuple(10, 3.14, "Hello");
    
    std::tie(a, b, c) = myTuple;

    std::cout << "Int value: " << a << std::endl;
    std::cout << "Double value: " << b << std::endl;
    std::cout << "String value: " << c << std::endl;
    
    return 0;
}

(3)使用结构化绑定(C++17):

#include <iostream>
#include <tuple>

int main() {
   
    std::tuple<int, double, std::string> myTuple(10, 3.14, "Hello");
    
    auto [intValue, doubleValue, stringValue] = myTuple;

    std::cout << "Int value: " << intValue << std::endl;
    std::cout << "Double value: " << doubleValue << std::endl;
    std::cout << "String value: " << stringValue << std::endl;
    
    return 0;
}

注意,std::forward_as_tuple的初始化方式是无法使用结构化绑定访问元素的。
2.3、效果展示
示例:

#include <tuple>
#include <iostream>
# include <string>
int main(int n,char ** args)
{
   
    // 直接初始化:
    std::tuple<int, double, std::string> mtuple(1,2.0,"3a");
    // 使用`std::make_tuple`函数进行初始化:
    auto mtuple2 = std::make_tuple(11,22.0,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想要入门的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值