C++ tuple(STL tuple)模板用法詳解

tupleC++ 11新引進的 build-in structure,但其實在其餘語言中tuple的使用已經行之有年(e.g. Javascript和Python中都有tuple)。C++ 11中tuple的引進是爲了下降不一樣programming languages之間的隔閡,比方說有些programmer於本是寫Python的,他在Python的世界中用tuple已經很習慣了,可是當他今天轉行來寫C++時,若是本來在python裏熟悉且經常使用的東西已經不存在了,這就會增長他轉行寫C++的難度與門檻。而C#在.NET 4.0後也開始support tuple。javascript

另外,tuple 這個術語也適用於不少其餘的場景,例如數據庫,這裏一個 tuple 就是由一些類型的不一樣數據項組成的,這和 tuple 的概念類似。tuple 對象有不少用途。當須要將多個對象看成一個對象傳給函數時,tuple 類型是頗有用的。html

在介紹C++ 11的tuple以前,咱們先來看其餘語言的tuple:java

Tuple in Javascript and Python

\[=====\ Tuple\ in\ Javascript ===== \]

function Tuple_In_JavaScript(val){
    var tuple = [24,"Kanna",[13.1,"Rio"]];
    alert(tuple[0]);
    alert(tuple[1]);
    alert(tuple[2][0]);
    alert(tuple[2][1]);
}

\[=====\ Tuple\ in\ Python ===== \]

tuple = (241,"Kanna",13.1);
print(tuple[0])
print(tuple[1])
print(tuple[2])

簡單的來講,tuple就是一組有序的值(an ordered set of values)。在關係型數據庫(relational database)中,某個table的一個row也能夠稱爲tuple;而在programming language中,tuple亦能夠用來儲存從database table中撈出來的row。除此以外,tuple也可讓function用來return 2個以上的return value。python

C++ 11中tuple的使用

代碼:ios

// #include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <tuple>
using namespace std;

void UsePair() {
    cout << "====UsePair====\n";
    pair<int, string> p1 = {1, "Kanna"};
    pair<float, char> p2 = make_pair(13.1f, 'a');

    cout << p1.first << " " << p1.second << endl;
    cout << p2.first << " " << p2.second << endl;

    cout << "\n";
}

void UseTuple() {
    cout << "====UseTuple====\n";
    tuple<int, double, string> t1(1, 13.1, "RioTian");
    cout << get<0>(t1) << endl;
    cout << get<1>(t1) << endl;
    cout << get<2>(t1) << endl;

    // 使用 tuple 頭文件中輔助函數 make_tuple() 生成tuple 對象
    tuple<int, float, char> t2 = make_tuple(2, 15.6f, 'a');
    cout << get<0>(t2) << endl;
    cout << get<1>(t2) << endl;
    cout << get<2>(t2) << endl;
}

int main() {
    UsePair();
    UseTuple();

    return 0;
}

對C++的programming來講對pair應該不陌生,tuple可視爲pair的通常化。比較麻煩的是用tuple時必須使用std::get來取得tuple內的值。c++

Initialization of a tuple:數據庫

// #include <bits/stdc++.h>
#include <iostream>
#include <tuple>
using namespace std;

void UseTuple() {
    // error, until C++17
    tuple<int, float, char> t3 = {2, 35.2f, 'a'};
    cout << get<0>(t2) << endl;
    cout << get<1>(t2) << endl;
    cout << get<2>(t2) << endl;
}

tuple<int, int> ReturnTuple() {
    // error, until C++17
    return {5, 6};

    // in C++ 11, we can only use
    // return std::make_tuple(5, 6);
}

int main() {
    UseTuple();
    ReturnTuple();
    return 0;
}

在C++ 11中,咱們已經能夠透過std::vector v = {1, 2, 3, 4};輕易地來初始化vector。可是tuple必需要到C++ 17後纔會support這個初始化的方式。因此在執行上面範例時會產生compile error。函數

tuple 基本操做

\[=====\ tuple\ 基本操做 ===== \]

tuple的建立和初始化

std::tuple<T1, T2, TN> t1;            //建立一個空的tuple對象(使用默認構造),它對應的元素分別是T1和T2...Tn類型,採用值初始化。
std::tuple<T1, T2, TN> t2(v1, v2, ... TN);    //建立一個tuple對象,它的兩個元素分別是T1和T2 ...Tn類型; 要獲取元素的值須要經過tuple的成員get<Ith>(obj)進行獲取(Ith是指獲取在tuple中的第幾個元素,請看後面具體實例)。
std::tuple<T1&> t3(ref&); // tuple的元素類型能夠是一個引用
std::make_tuple(v1, v2); // 像pair同樣也能夠經過make_tuple進行建立一個tuple對象

tuple的元素類型爲引用:ui

std::string name;
std::tuple<string &, int> tpRef(name, 30);
// 對tpRef第一個元素賦值,同時name也被賦值 - 引用
std::get<0>(tpRef) = "Sven";
 
// name輸出也是Sven
std::cout << "name: " << name << '\n';

有關tuple元素的操做spa

  1. 等價結構體

    開篇講過在某些時候tuple能夠等同於結構體同樣使用,這樣既方便又快捷。如:

struct person {
    char *m_name;
    char *m_addr;
    int  *m_ages;
};

//能夠用tuple來表示這樣的一個結構類型,做用是同樣的。
std::tuple<const char *, const char *, int>
  1. 如何獲取tuple元素個數

當有一個tuple對象但不知道有多少元素能夠經過以下查詢:

// tuple_size
#include <iostream>     // std::cout
#include <tuple>        // std::tuple, std::tuple_size
 
int main ()
{
  std::tuple<int, char, double> mytuple (10, 'a', 3.14);
 
  std::cout << "mytuple has ";
  std::cout << std::tuple_size<decltype(mytuple)>::value;
  std::cout << " elements." << '\n';
 
  return 0;
}
 
//輸出結果:
mytuple has 3 elements

3.如何獲取元素的值

獲取tuple對象元素的值能夠經過get (obj)方法進行獲取;

Ith - 是想獲取的元素在tuple對象中的位置。

obj - 是想獲取tuple的對象

// tuple_size
#include <iostream>     // std::cout
#include <tuple>        // std::tuple, std::tuple_size
 
int main ()
{
  std::tuple<int, char, double> mytuple (10, 'a', 3.14);
 
  std::cout << "mytuple has ";
  std::cout << std::tuple_size<decltype(mytuple)>::value;
  std::cout << " elements." << '\n';
 
  //獲取元素
  std::cout << "the elements is: ";
  std::cout << std::get<0>(mytuple) << " ";
  std::cout << std::get<1>(mytuple) << " ";
  std::cout << std::get<2>(mytuple) << " ";
 
  std::cout << '\n';
 
  return 0;
}
 
//輸出結果:
mytuple has 3 elements.
the elements is: 10 a 3.14

tuple不支持迭代,只能經過元素索引(或tie解包)進行獲取元素的值。可是給定的索引必須是在編譯器就已經給定,不能在運行期進行動態傳遞,不然將發生編譯錯誤:

for(int i=0; i<3; i++)
    std::cout << std::get<i>(mytuple) << " "; //將引起編譯錯誤

4.獲取元素的類型

要想獲得元素類型能夠經過tuple_element方法獲取,若有如下元組對象:

std::tuple<std::string, int> tp("Sven", 20);
 
// 獲得第二個元素類型
 
std::tuple_element<1, decltype(tp)>::type ages;  // ages就爲int類型
 
ages = std::get<1>(tp);
 
std::cout << "ages: " << ages << '\n';
 
//輸出結果: 
ages: 20

5.利用tie進行解包元素的值

tie 函數的詳解Click here

如同pair同樣也是能夠經過tie進行解包tuple的各個元素的值。以下tuple對象有4個元素,經過tie解包將會把這4個元素的值分別賦值給tie提供的4個變量中。

#include <iostream>
#include <tuple>
#include <utility>
 
int main(int argc, char **argv) {
    std::tuple<std::string, int, std::string, int> tp;
    tp = std::make_tuple("Sven", 25, "Shanghai", 21);
 
    // 定義接收變量
    std::string name;
    std::string addr;
    int ages;
    int areaCode;
 
    std::tie(name, ages, addr, areaCode) = tp;
    std::cout << "Output: " << '\n';
    std::cout << "name: " << name <<", ";
    std::cout << "addr: " << addr << ", ";
    std::cout << "ages: " << ages << ", ";
    std::cout << "areaCode: " << areaCode << '\n';
 
    return 0;
}
 
//輸出結果:
Output: 
name: Sven, addr: Shanghai, ages: 25, areaCode: 21

但有時候tuple包含的多個元素時只須要其中的一個或兩個元素,如此能夠經過std::ignore進行變量佔位,這樣將會忽略提取對應的元素。能夠修改上述例程:

#include <iostream>
#include <tuple>
#include <utility>
 
int main(int argc, char **argv) {
    std::tuple<std::string, int, std::string, int> tp;
    tp = std::make_tuple("Sven", 25, "Shanghai", 21);
 
    // 定義接收變量
    std::string name;
    std::string addr;
    int ages;
    int areaCode = 110;
 
    std::tie(name, ages, std::ignore, std::ignore) = tp;
    std::cout << "Output: " << '\n';
    std::cout << "name: " << name <<", ";
    std::cout << "addr: " << addr << ", ";
    std::cout << "ages: " << ages << ", ";
    std::cout << "areaCode: " << areaCode << '\n';
 
    return 0;
}
 
//輸出結果:
Output: 
name: Sven, addr: , ages: 25, areaCode: 110
  1. tuple元素的引用

    前面已經列舉了將引用做爲tuple的元素類型。下面經過引用搭配make_tuple()能夠提取tuple的元素值,將某些變量值設給它們,並經過改變這些變量來改變tuple元素的值:

#include <iostream>
#include <tuple>
#include <functional>
 
int main(int argc, char **agrv) {
 
    std::tuple<std::string, int, float> tp1("Sven Cheng", 77, 66.1);
 
    std::string name;
    int weight;
    float f;
 
    auto tp2 = std::make_tuple(std::ref(name), std::ref(weight), std::ref(f)) = tp1;
 
    std::cout << "Before change: " << '\n';
    std::cout << "name: " << name << ", ";
    std::cout << "weight: " << weight << ", ";
    std::cout << "f: " << f << '\n';
 
    name = "Sven";
    weight = 80;
    f = 3.14;
 
	std::cout << "After change: " << '\n';
	std::cout << "element 1st: " << std::get<0>(tp2) << ", ";
	std::cout << "element 2nd: " << std::get<1>(tp2) << ", ";
	std::cout << "element 3rd: " << std::get<2>(tp2) << '\n';
 
    return 0;
}
 
//輸出結果:
Before change: 
name: Sven Cheng, weight: 77, f: 66.1
After change: 
element 1st: Sven, element 2nd: 80, element 3rd: 3.14

參考

相關文章
相關標籤/搜索