網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
介紹
元組tuple是C++的一個(gè)模板,不同tuple類(lèi)型的成員類(lèi)型也不相同,但是一個(gè)tuple可以有任意數(shù)量的成員。
每個(gè)tuple類(lèi)型的成員個(gè)數(shù)是一定的,但是不同的tuple成員個(gè)數(shù)可以不同。
tuple的定義及初始化
使用tuple要引入tuple頭文件
#include <tuple>
tuple定義以及初始化(我們這里以成員類(lèi)型為int、string、vector為例子)
tuple<int,string,vector<int>> test{1,"hello,world",{4,5,6}};
當(dāng)然也可以使用它的構(gòu)造函數(shù)進(jìn)行初始化
tuple<int, string,vector<int>> test(1,"hello,world",{4,5,6});
存在這種情況,表面上看起來(lái)沒(méi)有進(jìn)行任何初始化操作,但實(shí)際上利用了不帶參數(shù)的默認(rèn)構(gòu)造函數(shù)
tuple<size_t, size_t> test; //每個(gè)成員的值為0
當(dāng)然如果你這樣定義后,又想對(duì)test的初始值所作更改,可以這樣寫(xiě),如下所示。(實(shí)際上現(xiàn)在的test不是原來(lái)的test,它被你新創(chuàng)建的tuple對(duì)象所替代了,嚴(yán)格意義來(lái)講并不算是初始化)
tuple<size_t, size_t> test; //每個(gè)成員的值為0
test = tuple<size_t, size_t>(1,2); //第一個(gè)成員的值為1,第二個(gè)成員的值為2
另外標(biāo)準(zhǔn)庫(kù)定義了make_tuple函數(shù)來(lái)生成tuple對(duì)象,我們可以用auto關(guān)鍵字來(lái)推測(cè)生成的tuple的類(lèi)型
auto test2 = make_tuple(3.14,"wasd");
如果要添加一個(gè)vector作為成員,則情況如下
//錯(cuò)誤寫(xiě)法
auto test2 = make_tuple(3.14,"wasd",{ 3,4,5 });
//正確寫(xiě)法
vector<int> nums = { 3,4,5 };
auto test2 = make_tuple(3.14,"wasd",nums);
tuple的使用
成員訪問(wèn)
說(shuō)起tuple的使用,最能直接想到的自然是對(duì)tuple的訪問(wèn)了。
在C++標(biāo)準(zhǔn)庫(kù)里,有一個(gè)名為get的函數(shù)模板。為了使用get,我們必須指定一個(gè)顯式模板實(shí)參來(lái)指示訪問(wèn)的第幾個(gè)成員,并在函數(shù)參數(shù)中給予它一個(gè)tuple對(duì)象。
tuple<int, string, vector<int>> test{ 1,"hello,world",{4,5,6} };
cout << get<0>(test) << endl; //打印test第一個(gè)成員,其類(lèi)型為int
cout << get<1>(test) << endl; //打印test第二個(gè)成員,其類(lèi)型為string
cout << get<2>(test)[0] << endl; //打印test第三個(gè)成員vector<int>的第一個(gè)元素
下面是返回結(jié)果
1
hello,world
4 ? ?
獲取tuple信息
可用以下方法獲取tuple元素個(gè)數(shù)
tuple<int, double, vector<string>> test{ 1,1.23,{"123","456"}};
//獲取tuple成員個(gè)數(shù)
size_t num = tuple_size<decltype(test)>::value;
//利用tuple某個(gè)成員類(lèi)型來(lái)聲明變量(以第0個(gè)成員為例)
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方法進(jìn)行解包,tie的參數(shù)個(gè)數(shù)與tuple成員個(gè)數(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比較
當(dāng)兩個(gè)tuple具有相同數(shù)目的成員,且對(duì)應(yīng)成員類(lèi)型可比較的時(shí)候,tuple比較才合法
//有如下四個(gè)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; //報(bào)錯(cuò),string與int類(lèi)型不能比較
test2 < test3; //報(bào)錯(cuò),test2與test3成員數(shù)量不同
test3 < test4; //正確,該語(yǔ)句值為T(mén)rue
tuple遍歷
tuple沒(méi)有迭代器,其遍歷非常麻煩,其設(shè)計(jì)目的也不在于此(如果項(xiàng)目開(kāi)發(fā)過(guò)程中要對(duì)某一數(shù)據(jù)結(jié)構(gòu)進(jìn)行遍歷該數(shù)據(jù)結(jié)構(gòu)應(yīng)盡量避免為tuple類(lèi)型,完全可以用list代替)
但此處還是給出遍歷的方法(不推薦使用)
本方法來(lái)自C++Tuple元組的詳細(xì)用法 - 知乎 (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開(kāi)發(fā)時(shí)的應(yīng)用
在項(xiàng)目開(kāi)發(fā)時(shí),如果我們想讓一個(gè)函數(shù)返回多個(gè)不同類(lèi)型的值的話可以使用tuple。
原文鏈接:https://juejin.cn/post/7108645986557231135
相關(guān)推薦
- 2023-12-18 MyBatisSystemException異常產(chǎn)生原因及解決方案
- 2022-08-26 教你用python從日期中獲取年、月、日和星期等30種信息_python
- 2022-09-03 Python標(biāo)準(zhǔn)庫(kù)time使用方式詳解_python
- 2023-02-15 EasyX繪制透明背景圖的方法詳解_C 語(yǔ)言
- 2022-04-26 C#新特性之可空引用類(lèi)型_C#教程
- 2022-04-12 iOS SDK中引入第三方頭文件報(bào)Undefined symbols for architectur
- 2023-05-06 Python寫(xiě)一個(gè)簡(jiǎn)單的api接口的實(shí)現(xiàn)_python
- 2022-01-19 el-table 定位 跳轉(zhuǎn) 至指定 行 位置,保證數(shù)據(jù)更新后位置不變
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支