std::tie

提供: C++入門
2013年12月27日 (金) 23:21時点におけるDaemon (トーク | 投稿記録)による版 (ページの作成:「std::tie とは、タプル(std::tuple)からアンッパックするために使われます。 '''読み方''' ;std::tie:えすてぃーでぃー たー...」)

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索
スポンサーリンク

std::tie とは、タプル(std::tuple)からアンッパックするために使われます。

読み方

std::tie
えすてぃーでぃー たーい

概要

std::tuple
複数の型の値を保持する
std::tie
要素をまとめて取り出す
std::ignore
特定要素を無視する

ヘッダファイル

#include <tuple>

tie1.cpp の例

ソースコード tie1.cpp

  1. std::make_tupleでタプルを作成します。
  2. tieでタプル t から値を取り出します。
#include <iostream>
#include <tuple>
#include <functional>
#include <string>
using namespace std;
int main(int argc, char const* argv[])
{
        auto    t = std::make_tuple(1, 'a', "foo");
        int     i;
        char    c;
        string  s;
        tie(i,c,s) = t;
        cout << i << endl;;
        cout << c << endl;;
        cout << s << endl;;
        return 0;
}

コンパイル

g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \
-Wl,-rpath=/usr/local/lib/gcc49  tie1.cpp -o tie1

実行例

% ./tie1
1
a
foo

タプルを展開するときに要素をスキップする

std::tieでタプルを展開するときに、std::ignoreを使用することで特定の要素をスキップできます。

関連項目




スポンサーリンク