網(wǎng)站首頁 編程語言 正文
介紹
元組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
相關(guān)推薦
- 2022-04-05 解決IDEA .properties文件中文亂碼的問題
- 2024-01-07 Plugin ‘org.springframework.boot:spring-boot-maven
- 2022-12-10 C++?vector與數(shù)組轉(zhuǎn)換寫入/讀出文件方式_C 語言
- 2022-07-22 Android Studio Arctic Fox 的不同之處
- 2023-11-19 如何卸載k3s?
- 2022-05-19 C++線程之thread詳解_C 語言
- 2023-01-01 C++?Boost.Signals2信號/槽概念_C 語言
- 2022-06-02 jquery實現(xiàn)界面點擊按鈕彈出懸浮框_jquery
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支