std::tuple
提供: cppreference.com
Defined in header <tuple>
|
||
template< class... Types > class tuple; |
(C++11およびそれ以降) | |
クラステンプレートstd::tuple
は、異なる型を持つ複数の値の固定サイズのコレクションです。std::pairを一般化したものと言えます。
目次 |
[編集] メンバ関数
新しいtuple をコンストラクトします (パブリックメンバ関数) | |
別のtuple の内容を代入します (パブリックメンバ関数) | |
2つのtuple の内容を交換します (パブリックメンバ関数) |
[編集] 非メンバ関数
引数の型で定義されるtuple 型のオブジェクトを作成します (関数テンプレート) | |
左辺値参照のtuple を作成します。tuple を個々のオブジェクトに分解することにも応用できます。 (関数テンプレート) | |
右辺値参照を内包するtuple を作成します。 (関数テンプレート) | |
任意の数のタプルを連結して新たなtuple を作成します。 (関数テンプレート) | |
タプルの指定された要素へのアクセサです。 (関数テンプレート) | |
辞書式順序に基いてタプルに内包される値を比較します。 (関数テンプレート) | |
(C++11) |
std::swapアルゴリズムのテンプレート特殊化です。 (関数テンプレート) |
[編集] ヘルパークラス
コンパイル時にtuple のサイズを取得します。 (クラステンプレートの特殊化の2つの値を比較します) | |
指定された要素の型を取得します。 (クラステンプレートの特殊化の2つの値を比較します) | |
std::uses_allocator型特性の特殊化です。 (クラステンプレートの特殊化の2つの値を比較します) | |
notes=(C++11) | |
tuple をtieで分解する時に要素をスキップするためのプレースホルダです。 (定数) |
[編集] 例
このコードを実行します
#include <tuple> #include <iostream> #include <string> #include <stdexcept> std::tuple<double, char, std::string> get_student(int id) { if (id == 0) return std::make_tuple(3.8, 'A', "Lisa Simpson"); if (id == 1) return std::make_tuple(2.9, 'C', "Milhouse Van Houten"); if (id == 2) return std::make_tuple(1.7, 'D', "Ralph Wiggum"); throw std::invalid_argument("id"); } int main() { auto student0 = get_student(0); std::cout << "ID: 0, " << "GPA: " << std::get<0>(student0) << ", " << "grade: " << std::get<1>(student0) << ", " << "name: " << std::get<2>(student0) << '\n'; double gpa1; char grade1; std::string name1; std::tie(gpa1, grade1, name1) = get_student(1); std::cout << "ID: 1, " << "GPA: " << gpa1 << ", " << "grade: " << grade1 << ", " << "name: " << name1 << '\n'; }
出力:
ID: 0, GPA: 3.8, grade: A, name: Lisa Simpson ID: 1, GPA: 2.9, grade: C, name: Milhouse Van Houten