亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++11新特性std::tuple的使用方法

 更新時間:2020年10月06日 09:28:22   作者:半杯茶的小酒杯  
這篇文章主要介紹了C++11新特性-std::tuple的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1. 引入頭文件

#include <tuple>

2. std::tuple初始化

std::tuple<int, std::string, float> t1(10, "Test", 3.14);

這里要注意,不是所有的C++ 11編譯器都支持copy-list-initialization的方式。如下代碼所示。

std::tuple<int, int> foo_tuple() 
{
 return {1, -1}; // Error until N4387
 return std::tuple<int, int>{1, -1}; // Always works
 return std::make_tuple(1, -1); // Always works
}

3. 打印std::tuple

打印std::tuple可以將它的元素逐個打印出來,不過非常繁瑣,我們可以通過如下通用的打印函數,幫助我們一次性的將tuple的所有要素打印出來。

#include <iostream>
#include <tuple>
#include <string>
 
// helper function to print a tuple of any size
template<class Tuple, std::size_t N>
struct TuplePrinter {
 static void print(const Tuple& t) 
 {
  TuplePrinter<Tuple, N-1>::print(t);
  std::cout << ", " << std::get<N-1>(t);
 }
};
 
template<class Tuple>
struct TuplePrinter<Tuple, 1> {
 static void print(const Tuple& t) 
 {
  std::cout << std::get<0>(t);
 }
};
 
template<typename... Args, std::enable_if_t<sizeof...(Args) == 0, int> = 0>
void print(const std::tuple<Args...>& t)
{
 std::cout << "()\n";
}
 
template<typename... Args, std::enable_if_t<sizeof...(Args) != 0, int> = 0>
void print(const std::tuple<Args...>& t)
{
 std::cout << "(";
 TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
 std::cout << ")\n";
}
// end helper function
 
int main()
{
 std::tuple<int, std::string, float> t1(10, "Test", 3.14);
 print(t1);
}

輸出:

(10, Test, 3.14)

4、合并多個std::tuple

std::tuple_cat函數可以將多個std::tuple合并為一個tuple。

int main()
{
 std::tuple<int, std::string, float> t1(10, "Test", 3.14);
 int n = 7;
 auto t2 = std::tuple_cat(t1, std::make_tuple("Foo", "bar"), t1, std::tie(n));
 n = 42;
 print(t2);
}

輸出:

(10, Test, 3.14, Foo, bar, 10, Test, 3.14, 42)

5. std::tuple的解包(unpack)

std::tie能夠將std::tuple包含的要素解包(unpack)成單個的對象。

#include <iostream>
#include <tuple>
#include <string>

int main() {
 auto info = std::make_tuple(3.8, 'A', "Lisa Simpson");
 
 double score = 0.0;
 char grade;
 std::string name;
 std::tie(score, grade, name) = info;

 std::cout << "score:" << score << ", grade:" << grade << ", name:" << name << std::endl;

return 0;
}

輸出:

score:3.8, grade:A, name:Lisa Simpson

std::tie還支持std::pair對象的解包(unpack)。

#include <iostream>
#include <tuple>
#include <string>
#include <utility>

int main() {
 auto info = std::make_pair(3.8, "Lisa Simpson");
 
 double score = 0.0;
 std::string name;
 std::tie(score, name) = info;

 std::cout << "score:" << score << ", name:" << name << std::endl;

return 0;
}

輸出:

score:3.8, name:Lisa Simpson

當我們不關注tuple中的某個元素時,可以使用std::ignore忽略該元素。

#include <iostream>
#include <tuple>
#include <string>
#include <utility>

int main() {
 auto info = std::make_pair(3.8, "Lisa Simpson");
 
 double score = 0.0;
 std::string name;
 std::tie(score, std::ignore) = info;

 std::cout << "score:" << score << ", name:" << name << std::endl;

return 0;
}

輸出:

score:3.8, name:

參考材料

https://en.cppreference.com/w/cpp/utility/tuple/tuple_cat

到此這篇關于C++11新特性-std::tuple的使用方法的文章就介紹到這了,更多相關C++11 std::tuple內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C語言游戲必備:光標定位與顏色設置的實現方法

    C語言游戲必備:光標定位與顏色設置的實現方法

    本篇文章是對c語言中光標定位與顏色設置的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • java string對象上的操作,常見的用法你知道嗎

    java string對象上的操作,常見的用法你知道嗎

    今天給大家?guī)淼氖顷P于Java的相關知識,文章圍繞著Java String類用法展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-08-08
  • C語言實現繪制貝塞爾曲線的函數

    C語言實現繪制貝塞爾曲線的函數

    貝塞爾曲線,又稱貝茲曲線或貝濟埃曲線,是應用于二維圖形應用程序的數學曲線。本文將利用C語言實現繪制貝塞爾曲線的函數,需要的可以參考一下
    2022-12-12
  • C++實現LRU緩存的操作方法

    C++實現LRU緩存的操作方法

    LRU是一種常用的緩存淘汰策略,主要目的是在緩存空間有限的情況下,優(yōu)先淘汰那些最長時間沒有被訪問的數據項,這篇文章主要介紹了C++實現LRU緩存,需要的朋友可以參考下
    2024-07-07
  • static全局變量與普通的全局變量的區(qū)別詳細解析

    static全局變量與普通的全局變量的區(qū)別詳細解析

    以下是對static全局變量與普通的全局變量的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-09-09
  • Dashboard Interface 應用實現操作

    Dashboard Interface 應用實現操作

    Dashboard Server Remote Control Interface是一個關鍵的功能,它為用戶提供了通過TCP/IP協(xié)議遠程控制機器人的能力,執(zhí)行包括開關機、加載程序、檢查機器人狀態(tài)以及設置機器人操作模式等多種操作,本文介紹Dashboard Interface 應用操作,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • C++同步線程實現示例詳解

    C++同步線程實現示例詳解

    這篇文章主要介紹了C++同步線程實現示例,線程同步是指同一進程中的多個線程互相協(xié)調工做從而達到一致性。之因此須要線程同步,是由于多個線程同時對一個數據對象進行修改操做時,可能會對數據形成破壞
    2022-11-11
  • C語言攝氏度互相轉換華氏

    C語言攝氏度互相轉換華氏

    這篇文章主要介紹了C語言攝氏度互相轉換華氏,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • C語言中字符的輸入輸出以及計算字符個數的方法詳解

    C語言中字符的輸入輸出以及計算字符個數的方法詳解

    這篇文章主要介紹了C語言中字符的輸入輸出以及計算字符個數的方法,是C語言入門學習中的基礎知識,需要的朋友可以參考下
    2015-11-11
  • c++中拷貝構造函數的參數類型必須是引用

    c++中拷貝構造函數的參數類型必須是引用

    如果拷貝構造函數中的參數不是一個引用,即形如CClass(const CClass c_class),那么就相當于采用了傳值的方式(pass-by-value),而傳值的方式會調用該類的拷貝構造函數,從而造成無窮遞歸地調用拷貝構造函數。因此拷貝構造函數的參數必須是一個引用
    2013-07-07

最新評論