c++ 11 tuple

tuple元組定義了一個有固定數目元素的容器,其中的每個元素類型都可以不相同,這與其他容器有著本質的區別.是對pair的泛化。

首先來介紹元組的創建和元組元素的訪問。通過make_tuple()創建元組,通過get<>()來訪問元組的元素。通過下面這段程式來認識這兩個函式的用法:
#include <iostream>
#include <tuple>
#include <functional>
int main()
{
auto t1 = std::make_tuple(10, "Test", 3.14);
std::cout << "The value of t1 is "
<< "(" << std::get<0>(t1) << ", " << std::get<1>(t1)
<< ", " << std::get<2>(t1) << ")\n";
int n = 1;
auto t2 = std::make_tuple(std::ref(n), n);//ref表示引用
n = 7;
std::cout << "The value of t2 is "
<< "(" << std::get<0>(t2) << ", " << std::get<1>(t2) << ")\n";
}
運行結果為:
The value of t1 is (10, Test, 3.14)
The value of t2 is (7, 1)

相關詞條

熱門詞條

聯絡我們