• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

cppのtuple

date: 2022-07-02 excerpt: cppのtupleの使い方

tag: c++cpptuple


cppのtupleの使い方

概要

  • C++で扱えるタプル
  • pythonと同じ様にタプル同士の比較も可能

具体例

#include <iostream>
#include <tuple>
#include <string>
#include <cassert>
#include <set>
using namespace std;

int main()
{
    // tupleの作成
    tuple<int, char, string> t = make_tuple(1, 'a', "hello");

    // 要素の取り出し
    assert(get<0>(t) == 1);
    assert(get<2>(t) == "hello");

    //tupleをunbox
    auto [i,c,str] = t;

    // tupleは比較可能
    set s{
        make_tuple(3, 'c', "ccc"),
        make_tuple(1, 'a', "aaa"),
        make_tuple(2, 'b', "bbb"),
    };
    for(auto [i0, c0, str0]: s) {
        cout << i0 << " " << c0 << " " << str0 << endl;
        /* 1 a aaa
         * 2 b bbb
         * 3 c ccc
         * */
    }
}

参考

  • std::tuple/cpprefjp - C++日本語リファレンス


c++cpptuple Share Tweet