名前空間
変種
操作

std::tuple

提供: cppreference.com
< cpp‎ | utility
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ(C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
tuple
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
 
ヘッダ <tuple> で定義
template<class... Types>
class tuple;
(C++11以上)

クラステンプレート std::tuple は、異なる型を持つ複数の値の固定サイズのコレクションです。 std::pair を一般化したものと言えます。

(std::is_trivially_destructible_v<Types>&& ...)true であれば、 tuple のデストラクタもトリビアルです。

(C++17以上)

目次

[編集]テンプレート引数

Types... - タプルが格納する要素の型。 空リストでも構いません。

[編集]メンバ関数

新しい tuple を構築します
(パブリックメンバ関数)
tuple の内容を別の tuple に代入します
(パブリックメンバ関数)
2つの tuple の内容を交換します
(パブリックメンバ関数)

[編集]非メンバ関数

引数の型によって定義される型の tuple オブジェクトを作成します
(関数テンプレート)[edit]
左辺値参照の tuple を作成したり、タプルを個々のオブジェクトに分解したりします
(関数テンプレート)[edit]
転送参照tuple を作成します
(関数テンプレート)[edit]
任意の数のタプルを連結して新たな tuple を作成します
(関数テンプレート)[edit]
タプルの指定された要素にアクセスします
(関数テンプレート)[edit]
(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20)
タプル内の値を辞書的に比較します
(関数テンプレート)[edit]
std::swap アルゴリズムの特殊化
(関数テンプレート)[edit]

[編集]ヘルパークラス

コンパイル時に tuple のサイズを取得します
(クラステンプレートの特殊化)[edit]
指定された要素の型を取得します
(クラステンプレートの特殊化)[edit]
std::uses_allocator 型特性の特殊化
(クラステンプレートの特殊化)[edit]
tupletie で分解するときに要素をスキップするためのプレースホルダです
(定数)[edit]

[編集]推定ガイド(C++17以上)

[編集]ノート

C++17 以前では、関数の戻り値にリスト初期化を使うことはできませんでした。

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

[編集]

#include <tuple>#include <iostream>#include <string>#include <stdexcept>   std::tuple<double, char, std::string> get_student(int id){if(id ==0)returnstd::make_tuple(3.8, 'A', "Lisa Simpson");if(id ==1)returnstd::make_tuple(2.9, 'C', "Milhouse Van Houten");if(id ==2)returnstd::make_tuple(1.7, 'D', "Ralph Wiggum");throwstd::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]
close