std::tuple
提供: cppreference.com
ヘッダ <tuple> で定義
|
||
template< class... Types > class tuple; |
(C++11以上) | |
クラステンプレート std::tuple
は、異なる型を持つ複数の値の固定サイズのコレクションです。 std::pair を一般化したものと言えます。
(std::is_trivially_destructible_v<Types> && ...) が true であれば、 |
(C++17以上) |
目次 |
[編集] テンプレート引数
Types... | - | タプルが格納する要素の型。 空リストでも構いません。 |
[編集] メンバ関数
新しい tuple を構築します (パブリックメンバ関数) | |
tuple の内容を別の tuple に代入します (パブリックメンバ関数) | |
2つの tuple の内容を交換します (パブリックメンバ関数) |
[編集] 非メンバ関数
引数の型によって定義される型の tuple オブジェクトを作成します (関数テンプレート) | |
左辺値参照の tuple を作成したり、タプルを個々のオブジェクトに分解したりします (関数テンプレート) | |
転送参照の tuple を作成します (関数テンプレート) | |
任意の数のタプルを連結して新たな tuple を作成します (関数テンプレート) | |
タプルの指定された���素にアクセスします (関数テンプレート) | |
(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20) |
タプル内の値を辞書的に比較します (関数テンプレート) |
(C++11) |
std::swap アルゴリズムの特殊化 (関数テンプレート) |
[編集] ヘルパークラス
コンパイル時に tuple のサイズを取得します (クラステンプレートの特殊化) | |
指定された要素の型を取得します (クラステンプレートの特殊化) | |
std::uses_allocator 型特性の特殊化 (クラステンプレートの特殊化) | |
tuple を tie で分解するときに要素をスキップするためのプレースホルダです (定数) |
[編集] 推定ガイド(C++17以上)
[編集] ノート
C++17 以前では、関数の戻り値にリスト初期化を使うことはできませんでした。
std::tuple<int, int> foo_tuple() { return {1, -1}; // Error until C++17 return std::make_tuple(1, -1); // Always works }
[編集] 例
Run this code
#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'; // C++17 structured binding: auto [ gpa2, grade2, name2 ] = get_student(2); std::cout << "ID: 2, " << "GPA: " << gpa2 << ", " << "grade: " << grade2 << ", " << "name: " << name2 << '\n'; }
出力:
ID: 0, GPA: 3.8, grade: A, name: Lisa Simpson ID: 1, GPA: 2.9, grade: C, name: Milhouse Van Houten ID: 2, GPA: 1.7, grade: D, name: Ralph Wiggum
[編集] 参考文献
- C++11 standard (ISO/IEC 14882:2011):
- 20.4 Tuples [tuple]