c++ tuple的操作

本文介绍了C++中的tuple操作,包括初始化、特殊特性及输出方法。详细讲解了tuple的初始化过程,强调了class tuple<>构造函数的显式特性,不支持隐式转换。同时探讨了tuple的其他特性,并展示了如何进行tuple的输出操作。

tuple(不定数值组)的操作
1.初始化 见Init()//class tuple<>构造函数是
explicit的,因此不支持隐式转换
2.其他tuple的特性 见special
3.tuple的输出 见output()等

#include<iostream>
#include<tuple>
using namespace std;
void Init()
{
    //常规初始化
    tuple<int, double, string> t1(3, 3.14, "Wh");//...
    //用pair初始化tuple
    pair<int, double> p(2, 3);
    tuple<int, double> t2(p);
    //other
    int a = 3;
    double b = 3.14;
    //tie()更多见 tieIntuple()
    tuple<int&, double&> p3 = tie(a, b);
    auto p4 = make_tuple(ref(a), ref(b));
}
void tieIntuple()
{
    int a = 4;
    double b = 4.2;
    //tie返回的类型是tuple<int&,double&>
    tuple<int, double> p1 = tie(a, b);
    tuple<int&, double&> p2 = tie(a, b);
    --a;
    b = 5.3;
    cout << get<0>(p1) << ends << get<1>(p1) << endl;
    cout << get<0>(p2) << ends << get<1>(p2) << endl;
    //通过tie()得到特定位置的值
    double d = 0.0;
    tie(ignore, d) = p2;
    cout << d << endl;
}
void special()
{
    //得到类型个数
    cout << tuple_size < tuple<int, double, double>>::value;
    //得到指定位置类型
    tuple_element<1, tuple<int, double, double>>::type d = 3.14;
    cout << d << endl;
    //将多个tuple串接成一个tuple
    auto tp = tuple_cat(make_tuple(3.22, 3), tie(d));
}
template<int Index,int Max,typename ...Args>
struct PRINT_TUPLE
{
    static void print(ostream& os, const tuple<Args...>& t)
    {
        os << get<Index>(t) << (Index + 1 == Max ? "" : ",");
        PRINT_TUPLE<Index + 1, Max, Args...>::print(os, t);
    }
};
template<int Max,typename ...Args>
struct PRINT_TUPLE<Max, Max, Args...>
{
    //无实例调用必须声明为静态方法
    static void print(ostream& os,const tuple<Args...>& t)
    {

    }
};
template<typename ...Args>
ostream& operator <<(ostream& os, const tuple<Args...>& t)
{
    os << "[";
    PRINT_TUPLE<0, sizeof...(Args), Args...>::print(os, t);
    return os << "]";
}
void output()
{
    tuple<int, double, double> t(3, 2.33, 3.22);
    cout << t;
}
int main()
{
    //Init();
    //tieIntuple();
    //special();
    output();
    system("pause");
    return 0;
}
### C++ 中 `tuple` 的详细用法及特性 #### 1. 基本概念 `tuple` 是 C++ 标准模板库(STL)中的一个容器,用于存储固定数量的不同类型的数据[^1]。与数组不同,`tuple` 可以存储不同类型的数据,且其大小在编译时确定。 #### 2. 定义与初始化 `tuple` 的定义方式如下: ```cpp #include <tuple> using namespace std; tuple<类型1, 类型2, ..., 类型N> 元组名; ``` 例如: ```cpp tuple<int, string, double> t; // 定义一个包含 int、string 和 double 的元组 ``` 可以通过以下几种方式初始化 `tuple`: - 使用直接赋值的方式初始化: ```cpp tuple<int, string, double> t = make_tuple(10, "Hello", 3.14); ``` - 使用花括号初始化: ```cpp tuple<int, string, double> t{10, "Hello", 3.14}; ``` #### 3. 访问元素 由于 `tuple` 中的元素可以是不同的类型,因此不能像数组那样通过索引访问元素。C++ 提供了两种方法来访问 `tuple` 中的元素: - **`get<索引>(tuple)`**:通过索引访问元素。 ```cpp int a = get<0>(t); // 获取第一个元素 string b = get<1>(t); // 获取第二个元素 double c = get<2>(t); // 获取第三个元素 ``` - **解构赋值**:从 C++17 开始,支持使用结构化绑定来解构 `tuple`。 ```cpp auto [x, y, z] = t; // 解构赋值 cout << x << " " << y << " " << z << endl; ``` #### 4. 修改元素 可以直接通过 `get` 函数修改 `tuple` 中的元素: ```cpp get<0>(t) = 20; // 修改第一个元素 get<2>(t) = 2.718; // 修改第三个元素 ``` #### 5. 比较操作 `tuple` 支持比较操作符(如 `<`, `>`, `==`, `!=` 等),按照字典序进行比较。 ```cpp tuple<int, string> t1 = {1, "Apple"}; tuple<int, string> t2 = {2, "Banana"}; if (t1 < t2) { cout << "t1 is less than t2" << endl; } ``` #### 6. 常用函数 - **`make_tuple`**:用于创建一个 `tuple` 对象。 ```cpp tuple<int, string, double> t = make_tuple(10, "Hello", 3.14); ``` - **`tie`**:将多个变量绑定到一个 `tuple` 上,用于解包。 ```cpp int a; string b; double c; tie(a, b, c) = t; // 将 tuple 中的元素解包到变量 a, b, c 中 ``` - **`swap`**:交换两个 `tuple` 的内容。 ```cpp tuple<int, string> t1 = {1, "Apple"}; tuple<int, string> t2 = {2, "Banana"}; swap(t1, t2); // 交换 t1 和 t2 的内容 ``` #### 7. 示例代码 以下是一个完整的示例,展示 `tuple` 的基本用法: ```cpp #include <iostream> #include <tuple> using namespace std; int main() { // 创建并初始化 tuple tuple<int, string, double> t = make_tuple(10, "Hello", 3.14); // 访问元素 cout << "First element: " << get<0>(t) << endl; cout << "Second element: " << get<1>(t) << endl; cout << "Third element: " << get<2>(t) << endl; // 修改元素 get<0>(t) = 20; get<2>(t) = 2.718; // 使用结构化绑定(C++17) auto [x, y, z] = t; cout << "After modification: " << x << ", " << y << ", " << z << endl; // 比较 tuple tuple<int, string> t1 = {1, "Apple"}; tuple<int, string> t2 = {2, "Banana"}; if (t1 < t2) { cout << "t1 is less than t2" << endl; } return 0; } ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值