網站首頁 編程語言 正文
C++11, 14, 17對tuple元素的訪問
std::tuple 作為可以存放任意個數,任意類型的元祖被我個人經常使用。
記得以前看侯捷談到這個數據結構的時候,被他的實現所驚奇,太巧妙地設計。
我自己在使用std::tuple的時候也隨著C++版本的更新嘗試新的寫法,對于元組中元素的獲取,我一直覺得很有意思:
比如我有這么一個源文件:
#include <iostream>
#include <tuple>
#define CPP11 (__cplusplus < 201401L)
#define CPP14 (__cplusplus < 201701L and __cplusplus > 201401L)
#define CPP17 (__cplusplus >= 201702L)
class Moo {
Moo() { ::printf("%s\n", __PRETTY_FUNCTION__); }
Moo(Moo const &) { ::printf("%s\n", __PRETTY_FUNCTION__); }
Moo(Moo &&) { ::printf("%s\n", __PRETTY_FUNCTION__); }
Moo &operator=(Moo const &) noexcept {
::printf("%s\n", __PRETTY_FUNCTION__);
return *this;
}
Moo &operator=(Moo &&) noexcept {
::printf("%s\n", __PRETTY_FUNCTION__);
return *this;
}
~Moo() { ::printf("%s\n", __PRETTY_FUNCTION__); }
};
int main() {
std::cout << "c++ version:" << __cplusplus << std::endl;
auto &&tp = std::make_tuple<Moo, const char *, int>(Moo(), "hello world", 3);
#if CPP11
auto &&first = std::get<0>(tp);
auto &&second = std::get<1>(tp);
auto &&third = std::get<2>(tp);
#endif
#if CPP14
auto &&first = std::get<Moo>(tp);
auto &&second = std::get<const char *>(tp);
auto &&third = std::get<int>(tp);
#endif
#if CPP17
auto [a, b, c] = tp;
auto &[first, second, third] = tp;
#endif
return 0;
}
Moo類考察當前對象在構造tuple和返回值的過程中經歷怎樣的人生
- C++11: 使用std::get方法來獲取對應秩的元素的引用
- C++14: 使用類似在nlohmannjson里面獲取對應類型的json對象的方式,通過類型來獲取對應的元素的引用,有點現代C++那味兒了吼
- C++17: 引入了結構化綁定,不實用引用的時候會返回新創建的臨時對象,a, b, c是對臨時對象的右值引用,而使用引用則是想std::get那樣引用原本對象。C++17結構化綁定yyds!表達力比之前強太多
std::tuple大總結
C++11引入了一個新的較實用的模板類型,std::tuple,也即是元組。元組是一個固定大小的不同類型(異質,heterogeneous)值的集合,也即它可以同時存放不同類型的數據。
類似于python中用小括號表示的元組類型。C++已有的std::pair類型類似于一個二元組,可看作是std::tuple的一個特例,std::tuple也可看作是std::pair的泛化。std::pair的長度限制為2,而std::tuple的元素個數為0~任意個。
元組的使用
- 有時候希望將一些數據組合成單一對象,但又不想麻煩地定義一個新數據結構來表示這些數據時,std::tuple是非常有用的。我們可以將std::tuple看作一個“快速而隨意”的數據結構。我們可以把它當作一個通用的結構體使用,但又不需要創建和獲取結構體的特征,使得程序更加簡潔直觀。
- 創建一個std::tuple對象時,可以使用tuple的默認構造函數,它會對每個成員進行值初始化;也可以為每個成員提供一個初始值,此時的構造函數是explicit的,因此必須使用直接初始化方法。
- 一個std::tuple類型的成員數目是沒有限制的,因此,元組的成員都是未命名的。要訪問一個元組的成員,就要使用一個名為get的標準庫函數模板。為了使用get,我們必須指定一個顯式模板實參,它指出我們想要訪問第幾個成員。我們傳遞給get一個tuple對象,它返回指定成員的引用。get尖括號中的值必須是一個整型常量表達式。與往常一樣,我們從0開始計數,意味著get<0>是第一個成員。
- 為了使用tuple_size或tuple_element,我們需要知道一個元組對象的類型。與往常一樣,確定一個對象的類型的最簡單方法就是使用decltype。
- std::tuple的一個常見用途是從一個函數返回多個值。
- std::tuple中元素是被緊密地存儲的(位于連續的內存區域),而不是鏈式結構。
- std::tuple實現了多元組,這是一個編譯期就確定大小的容器,可以容納不同類型的元素。多元組類型在當前標準庫中被定義為可以用任意數量參數初始化的類模板。每一模板參數確定多元組中一元素的類型。所以,多元組是一個多類型、大小固定的值的集合。
典型使用
創建和初始化
{
? ? std::tuple<int, double, std::string> first; ? ?// 創建一個空的元組,需要指定元組元素的數據類型,調用各個成員的默認構造函數進行初始化。
? ? std::tuple<int, double, std::string> second(first); ?// 拷貝構造
? ? std::tuple<int, char> third(10, 'a'); ? ? ? ?// 創建并初始化,使用小括號初始化
? ? std::tuple<int, std::string, double> fourth{42, "Test", -3.14}; ?// 創建并初始化,使用新的大括號初始化列表方式初始化
? ? std::tuple<int, char> fifth(std::make_tuple(20, 'b')); ? ? // 移動構造,使用模板庫的make_tuple
? ? first = std::make_tuple(1, 3.14, "tuple"); ? ? ? // 移動賦值
? ? int i_third = 3;
? ? std::tuple<int&> sixth(std::ref(i_third)); ? // 創建一個元組,元組的元素可以被引用
}?
元組的訪問和修改?
std::get<N>()
{
? ? int n = 1;
? ? auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
? ? // get尖括號中的值必須是一個整型常量表達式。從0開始計數,意味著get<0>是第一個成員。
? ? std::cout << "The value of t is " ?<< "("
? ? ? ? ? ? ? ?<< std::get<0>(t) << ", " << std::get<1>(t) << ", "
? ? ? ? ? ? ? ?<< std::get<2>(t) << ", " << std::get<3>(t) << ", "
? ? ? ? ? ? ? ?<< std::get<4>(t) << ")\n";
? ? // 由于get返回指定元素的引用,所以可用來修改指定位置的元素的值。此處因為第4個元素是引用類型,所以被引用的值也會改變
? ? std::get<3>(t) = 9;
? ? std::cout << n << std::endl;
}
元組的元素個數
使用std::tuple_size<>()
{
? ? std::tuple<char, int, long, std::string> first('A', 2, 3, "4");
? ? int i_count = std::tuple_size<decltype(first)>::value; ?// 使用std::tuple_size計算元組個數
? ? std::cout << "the number of elements of a tuple:" << i_count << "\n";
}
元組的解包
std::tie() 元組包含一個或者多個元素,使用std::tie解包: 首先需要定義對應元素的變量,再使用tie。
{ // std::tie: function template, Constructs a tuple object whose elements are references
? // to the arguments in args, in the same order
? // std::ignore: object, This object ignores any value assigned to it. It is designed to be used as an
? // argument for tie to indicate that a specific element in a tuple should be ignored.
? ??
? ? int myint;
? ? char mychar;
?
? ? std::tuple<int, float, char> mytuple;
?
? ? mytuple = std::make_tuple(10, 2.6, 'a'); ? ? ? ? ?// packing values into tuple
?
? ? std::tie(myint, std::ignore, mychar) = mytuple; ? // unpacking tuple into variables
?
? ? std::cout << "myint contains: " << myint << '\n';
? ? std::cout << "mychar contains: " << mychar << '\n';
}
元組的元素類型獲取
獲取元組中某個元素的數據類型,需要用到另外一個類型: std::tuple_element。 語法: std::tuple_element<index, tuple>。
{
? ? std::tuple<int, std::string> third(9, std::string("ABC"));
? ??
? ? // 得到元組第1個元素的類型,用元組第一個元素的類型聲明一個變量
? ? std::tuple_element<1, decltype(third)>::type val_1;
? ??
? ? // 獲取元組的第一個元素的值
? ? val_1 = std::get<1>(third);
? ? std::cout << "val_1 = " << val_1.c_str() << "\n";
}
元組的拼接
使用 std::tuple_cat 執行拼接
{
? ? std::tuple<char, int, double> first('A', 1, 2.2f);
? ? // 組合到一起, 使用auto, 自動推導
? ? auto second = std::tuple_cat(first, std::make_tuple('B', std::string("-=+")));
? ? // 組合到一起,可以知道每一個元素的數據類型時什么 與 auto推導效果一樣
? ? std::tuple<char, int, double, char, std::string> third = std::tuple_cat(first, std::make_tuple('B', std::string("-=+")));
? ? // 輸出合并后的元組內容
? ? int index = 0;
? ? std::cout << index++ << " = " << std::get<0>(second) << "\n"; ?// 0 = A
? ? std::cout << index++ << " = " << std::get<1>(second) << "\n"; ?// 1 = 1?
? ? std::cout << index++ << " = " << std::get<2>(second) << "\n"; ?// ?2 = 2.2
? ? std::cout << index++ << " = " << std::get<3>(second) << "\n"; ?// 3 = B
? ? std::cout << index++ << " = " << std::get<4>(second).c_str() << "\n"; // 4 = -=+
}
元組的遍歷
元組沒用提供operator []重載,遍歷起來較為麻煩,需要為其單獨提供遍歷模板函數?
原文鏈接:https://blog.csdn.net/qq_41540355/article/details/122149950
相關推薦
- 2022-04-11 error: failed to push some refs to解決方法
- 2022-06-25 CentOS?8?安裝調試KVM的詳細步驟_Kvm
- 2022-06-16 利用Jetpack?Compose實現繪制五角星效果_Android
- 2023-03-21 styled-components?性能詳解_React
- 2021-12-16 el-tree 設置選項框選中狀態,通過setCheckedKeys設置,會導致父選項框選中,子選項
- 2022-10-18 AJAX淺析數據交換的實現_AJAX相關
- 2022-02-27 selectByPrimaryKey()按主鍵查詢結果為null
- 2022-05-22 Nginx設置HTTPS的方法步驟_nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支