日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

淺析C++元組tuple類型_C 語言

作者:CairBin ? 更新時間: 2022-08-05 編程語言

介紹

元組tuple是C++的一個模板,不同tuple類型的成員類型也不相同,但是一個tuple可以有任意數(shù)量的成員。

每個tuple類型的成員個數(shù)是一定的,但是不同的tuple成員個數(shù)可以不同。

tuple的定義及初始化

使用tuple要引入tuple頭文件

#include <tuple>

tuple定義以及初始化(我們這里以成員類型為int、string、vector為例子)

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

當然也可以使用它的構(gòu)造函數(shù)進行初始化

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

存在這種情況,表面上看起來沒有進行任何初始化操作,但實際上利用了不帶參數(shù)的默認構(gòu)造函數(shù)

tuple<size_t, size_t> test;	//每個成員的值為0

當然如果你這樣定義后,又想對test的初始值所作更改,可以這樣寫,如下所示。(實際上現(xiàn)在的test不是原來的test,它被你新創(chuàng)建的tuple對象所替代了,嚴格意義來講并不算是初始化)

tuple<size_t, size_t> test;	//每個成員的值為0
test = tuple<size_t, size_t>(1,2);	//第一個成員的值為1,第二個成員的值為2

另外標準庫定義了make_tuple函數(shù)來生成tuple對象,我們可以用auto關(guān)鍵字來推測生成的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的函數(shù)模板。為了使用get,我們必須指定一個顯式模板實參來指示訪問的第幾個成員,并在函數(shù)參數(shù)中給予它一個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>的第一個元素

下面是返回結(jié)果

1
hello,world
4 ? ?

獲取tuple信息

可用以下方法獲取tuple元素個數(shù)

tuple<int, double, vector<string>> test{ 1,1.23,{"123","456"}};
//獲取tuple成員個數(shù)
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的參數(shù)個數(shù)與tuple成員個數(shù)一致,否則要用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具有相同數(shù)目的成員,且對應成員類型可比較的時候,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成員數(shù)量不同
test3 < test4;	//正確,該語句值為True

tuple遍歷

tuple沒有迭代器,其遍歷非常麻煩,其設計目的也不在于此(如果項目開發(fā)過程中要對某一數(shù)據(jù)結(jié)構(gòu)進行遍歷該數(shù)據(jù)結(jié)構(gòu)應盡量避免為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開發(fā)時的應用

在項目開發(fā)時,如果我們想讓一個函數(shù)返回多個不同類型的值的話可以使用tuple。

原文鏈接:https://juejin.cn/post/7108645986557231135

欄目分類
最近更新