C++11新特性std::make_tuple的使用
std::tuple是C++ 11中引入的一個(gè)非常有用的結(jié)構(gòu),以前我們要返回一個(gè)包含不同數(shù)據(jù)類型的返回值,一般都需要自定義一個(gè)結(jié)構(gòu)體或者通過(guò)函數(shù)的參數(shù)來(lái)返回,現(xiàn)在std::tuple就可以幫我們搞定。
1.引用頭文件
#include <tuple>
2. Tuple初始化
std::tuple的初始化可以通過(guò)構(gòu)造函數(shù)實(shí)現(xiàn)。
// Creating and Initializing a tuple std::tuple<int, double, std::string> result1 { 22, 19.28, "text" };
這種初始化方式要定義各個(gè)元素的數(shù)據(jù)類型,比較繁瑣,C++11也提供了另外一種方式std::make_tuple。
3. std::make_tuple
// Creating a tuple using std::make_tuple auto result2 = std::make_tuple( 7, 9.8, "text" );
這種初始化方式避免需要逐個(gè)指定元素類型的問(wèn)題,自動(dòng)化實(shí)現(xiàn)各個(gè)元素類型的推導(dǎo)。
完整例子一:
#include <iostream> #include <tuple> #include <string> int main() { // Creating and Initializing a tuple std::tuple<int, double, std::string> result1 { 22, 19.28, "text" }; // Compile error, as no way to deduce the types of elements in tuple //auto result { 22, 19.28, "text" }; // Compile error // Creating a tuple using std::make_tuple auto result2 = std::make_tuple( 7, 9.8, "text" ); // std::make_tuple automatically deduced the type and created tuple // Print values std::cout << "int value = " << std::get < 0 > (result2) << std::endl; std::cout << "double value = " << std::get < 1 > (result2) << std::endl; std::cout << "string value = " << std::get < 2 > (result2) << std::endl; return 0; }
輸出:
<strong>int</strong> value = 7
<strong>double</strong> value = 9.8
string value = text
完整例子二:
#include <iostream> #include <tuple> #include <functional> std::tuple<int, int> f() // this function returns multiple values { int x = 5; return std::make_tuple(x, 7); // return {x,7}; in C++17 } int main() { // heterogeneous tuple construction int n = 1; auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n); n = 7; 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"; // function returning multiple values int a, b; std::tie(a, b) = f(); std::cout << a << " " << b << "\n"; }
輸出:
The value of t is (10, Test, 3.14, 7, 1)
5 7
參考材料
https://thispointer.com/c11-make_tuple-tutorial-example/
https://en.cppreference.com/w/cpp/utility/tuple/make_tuple
到此這篇關(guān)于C++11新特性std::make_tuple的使用的文章就介紹到這了,更多相關(guān)C++11 std::make_tuple內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言中單鏈表(不帶頭結(jié)點(diǎn))基本操作的實(shí)現(xiàn)詳解
鏈表是一種物理存儲(chǔ)結(jié)構(gòu)上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過(guò)鏈表中的指針鏈接次序?qū)崿F(xiàn)的。本文主要和大家聊聊C語(yǔ)言中單鏈表(不帶頭結(jié)點(diǎn))的基本操作,感興趣的小伙伴可以了解一下2022-11-11C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之雙鏈表&循環(huán)鏈表&靜態(tài)鏈表詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中雙鏈表&循環(huán)鏈表&靜態(tài)鏈表的原理與使用,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09C++ 中將一維數(shù)組轉(zhuǎn)成多維的三種方式示例詳解
這篇文章主要介紹了C++ 中將一維數(shù)組轉(zhuǎn)成多維的三種方式,每種方式結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12C數(shù)據(jù)結(jié)構(gòu)中串簡(jiǎn)單實(shí)例
這篇文章主要介紹了C數(shù)據(jù)結(jié)構(gòu)中串簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06樹(shù)存儲(chǔ)結(jié)構(gòu)的幾種表示方法
今天小編就為大家分享一篇關(guān)于樹(shù)存儲(chǔ)結(jié)構(gòu)的幾種表示方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03基于QT設(shè)計(jì)一個(gè)春聯(lián)自動(dòng)生成器
春節(jié)是中國(guó)最隆重的傳統(tǒng)節(jié)日,一到過(guò)年家家戶戶肯定是要貼春聯(lián);在春節(jié)前夕,會(huì)用大紅紙張,加上濃墨書寫祝福詞語(yǔ)。本文將利用Qt框架設(shè)計(jì)一個(gè)春聯(lián)自動(dòng)生成器,需要的可以參考一下2022-01-01C++構(gòu)造函數(shù)初始化列表的實(shí)現(xiàn)詳解
構(gòu)造函數(shù)主要作用在于創(chuàng)建對(duì)象時(shí)為對(duì)象的成員屬性賦值,構(gòu)造函數(shù)由編譯器自動(dòng)調(diào)用,無(wú)須手動(dòng)調(diào)用;析構(gòu)函數(shù)主要作用在于對(duì)象銷毀前系統(tǒng)自動(dòng)調(diào)用,執(zhí)行一 些清理工作2022-09-09