c语言元组-c语言元组是什么

lxf2023-04-04 20:14:02
摘要

元组tuple是C++的一个模板,不同tuple类型的成员类型也不相同,但是一个tuple可以有任意数量的成员,今天通过本文给大家介绍C++元组tuple类型,感兴趣的朋友一起看看吧

目录
  • 介绍
  • tuple的定义及初始化
  • tuple的使用
    • 成员访问
    • 获取tuple信息
    • 拼接tuple
    • 交换tuple
    • tuple解包
    • tuple比较
    • tuple遍历
  • tuple开发时的应用

    介绍

    元组tuplec++的一个模板,不同tuple类型的成员类型也不相同,但是一个tuple可以有任意数量的成员。

    每个tuple类型的成员个数是一定的,但是不同的tuple成员个数可以不同。

    tuple的定义及初始化

    使用tuple要引入tuple头文件

    #include <tuple>

    tuple定义以及初始化(我们这里以成员类型为int、string、vector为例子

    tuple<int,string,vector<int>> test{1,"hello,world",{4,5,6}};

    当然也可以使用它的构造函数进行初始化

    tuple<int, string,vector<int>> test(1,"hello,world",{4,5,6});

    存在这种情况,表面上看起来没有进行任何初始化操作,但实际上利用了不带参数的默认构造函数

    tuple<size_t, size_t> test;	//每个成员的值为0

    当然如果你这样定义后,又想对test的初始值所作更改,可以这样写,如下所示。(实际上现在的test不是原来的test,它被你新创建的tuple对象所替代了,严格意义来讲并不算是初始化)

    tuple<size_t, size_t> test;	//每个成员的值为0
    test = tuple<size_t, size_t>(1,2);	//第一个成员的值为1,第二个成员的值为2

    另外标准库定义了make_tuple函数来生成tuple对象,我们可以用auto关键字来推测生成的tuple的类型

    auto test2 = make_tuple(3.14,"wasd");

    如果要添加一个vector作为成员,则情况如下

    //错误写法
    auto test2 = make_tuple(3.14,"wasd",{ 3,4,5 });
    //正确写法
    vector<int> nums = { 3,4,5 };
    auto test2 = make_tuple(3.14,"wasd",nums);

    tuple的使用

    成员访问

    说起tuple的使用,最能直接想到的自然是对tuple的访问了。

    在C++标准库里,有一个名为get的函数模板。为了使用get,我们必须指定一个显式模板实参来指示访问的第几个成员,并在函数参数中给予它一个tuple对象。

    tuple<int, string, vector<int>> test{ 1,"hello,world",{4,5,6} };
    cout << get<0>(test) << endl;		//打印test第一个成员,其类型为int
    cout << get<1>(test) << endl;		//打印test第二个成员,其类型为string
    cout << get<2>(test)[0] << endl;	//打印test第三个成员vector<int>的第一个元素

    下面是返回结果

    1
    hello,world
    4    

    获取tuple信息

    可用以下方法获取tuple元素个数

    tuple<int, double, vector<string>> test{ 1,1.23,{"123","456"}};
    //获取tuple成员个数
    size_t num = tuple_size<decltype(test)>::value;
    //利用tuple某个成员类型来声明变量(以第0个成员为例)
    tuple_element<0, decltype(test)>::type n = get<0>(test);

    拼接tuple

    tuple<int, int, int> test(1,2,3);
    tuple<int, int, int> test2(4,5,6);
    tuple<int, int, int> test3(7,8,9);
    //拼接
    auto new_tup = tuple_cat(test, test2, test3);

    交换tuple

    tuple<int, int, int> test(1,2,3);
    tuple<int, int, int> test2(4,5,6);
    //交换
    test.swap(test2);

    tuple解包

    tuple使用tie方法进行解包,tie的参数个数与tuple成员个数一致,否则要用ignore占位

    tuple<int,int> test{2,8};
    tuple<int,int> test2{2,8,6};
    int a, b;
    //解包
    tie(a, b) = test;
    //ignore占位的情况
    tie(a, b, ignore) = test2;

    tuple比较

    当两个tuple具有相同数目的成员,且对应成员类型可比较的时候,tuple比较才合法

    //有如下四个tuple
    tuple<string, string> test{"1","2"};
    tuple<int, int, int> test2{ 1,2,3 };
    tuple<int,int> test3{1,2};
    tuple<int, int> test4{ 4,5 };
    test == test2;	//报错,string与int类型不能比较
    test2 < test3;	//报错,test2与test3成员数量不同
    test3 < test4;	//正确,该语句值为True

    tuple遍历

    tuple没有迭代器,其遍历非常麻烦,其设计目的也不在于此(如果项目开发过程中要对某一数据结构进行遍历该数据结构应尽量避免为tuple类型,完全可以用list代替)

    但此处还是给出遍历的方法(不推荐使用)

    本方法来自C++Tuple元组的详细用法 - 知乎 (zhihu.com)

    #include <iOStream>
    #include <tuple>
    #include <array>
    #include <utility>
    using namespace std;
    template<class Tuple, size_t N>
    struct PrintTuple
    {
    	static void Printf(const Tuple& Value)
    	{
    		PrintTuple<Tuple, N - 1>::Printf(Value);
    		cout << "," << get<N - 1>(Value);
    	}
    };
    template<class Tuple>
    struct PrintTuple<Tuple, 1>
    {
    	static void Printf(const Tuple& Value)
    	{
    	     cout << get<0>(Value);
    	}
    };
    template<class... Args>
    void PrintfMyTuple(const tuple<Args...>& vlaue)
    {
          PrintTuple<decltype(vlaue), sizeof...(Args)>::Printf(vlaue);
    }
    int main()
    {
    	tuple<int, int, int, int> a(2, 3, 1, 4);
    	PrintfMyTuple(a);
    	system("pause");
    
    	return 0;
    }

    tuple开发时的应用

    在项目开发时,如果我们想让一个函数返回多个不同类型的值的话可以使用tuple。

    到此这篇关于C++元组tuple类型的文章就介绍到这了,更多相关C++元组tuple类型内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.adminjs.cn!