std::tuple (c++11)

c++11 で加わった std::tuple です。 
boost の tuple とは文法が違う気がします。ひょっとしたら std の仕様化に伴ってboostの方がすでに仕様変更されているかもしれません。

 

 

 

[環境]

コンパイラ : Visual Studio 2012,  
OS : Windows8 64bit 日本語版,  

 

 

1.tuple, get

もともとの標準ライブラリでは、単純に二つの型を格納するために定義された std:pair というクラスライブラリがあります。c++11で加わったtupleは、二つに限らず三つ、四つの値を単純に並べて格納することができます。
pair の要素にアクセスするためには、firstメンバ変数とsecondメンバ変数を使用しましたが、tupleの要素へアクセスするには std:get<N>(std::tuple) を使用します。

#include <iostream>
#include <string>
#include <tuple>

using namespace std;

int main(int argc, char* argv[])
{
    // int, string, float の 3要素tuple
    tuple<int, string, float> t( 123, "こんにちは", 4.56 );

    // 各要素を取得
    // boost の tuple と文法が違う気がします。boost では t.get<0>() でした。
    // boostの方がすでにstdの方へ合わせて仕様変更されているかもしれませんが。
    cout << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << endl;

    // 一部書き換え
    get<2>(t) *= get<0>(t);
    cout << get<2>(t) << endl;

    return 0;
}

出力結果

 

 

2.make_tuple, tie

std::make_tuple 関数を使用することで、渡された可変個のパラメータから tuple 型のオブジェクトを構築することができます。

 

#include <iostream>
#include <tuple>

using namespace std;

// +-*/の四則演算の結果を一度に返す関数
tuple<int, int, int, double> calc_4( int x, int y)
{
    // make_tuple関数で、四則演算の結果の tuple を作る
    return make_tuple( x+y, x-y, x*y, double(x)/y );
}

int _tmain(int argc, _TCHAR* argv[])
{
    int add, sub, mul ;
    double div;

    // tie で、変数への参照からなる4要素tupleを作成
    // 他で作ったtupleの各要素を別々の変数へ分解
    tie( add, sub, mul, div ) = calc_4( 1, 2 );

    cout << add << ", " << sub << ", " << mul << ", " << div << endl;

    return 0;
}

 

 

3.tie を使った 優先順位付ソート

優先順位付きで operator<() を定義することを行います。
Windowsのエクスプローラで「ファイル種別順に並べて、同じファイル種別のものはファイル名順に並べる」というような内容です。

参考URL: http://d.hatena.ne.jp/faith_and_brave/20121210/1355126432

#include <iostream>     // cout
#include <string>       // string
#include <vector>       // vector
#include <algorithm>    // sort
#include <tuple>        // tie


struct File {
    std::string type;
    std::string name;

    File(const std::string& type, const std::string& name)
        : type(type), name(name){}
};

bool operator<(const File& a, const File& b)
{
    // ファイル種別、ファイル名の順番で優先順位を付けて比較
    return std::tie( a.type, a.name ) < std::tie( b.type, b.name );
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<File> files;

    files.push_back( File("text", "b.txt") );
    files.push_back( File("application", "b.exe") );
    files.push_back( File("application", "a.exe") );
    files.push_back( File("text", "a.txt") );

    // 並べ替え
    std::sort(files.begin(), files.end());

    for (const File& file : files) {
        std::cout << file.type << ", " << file.name << std::endl;
    }
}

 

 

ライセンス

本ページの情報は、特記無い限り下記 MIT ライセンスで提供されます。

The MIT License (MIT)

Copyright © 2012-2022 Kinoshita Hidetoshi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 

 

参考

 


 

変更履歴

2022-07-12 - デザインなど更新
2012-12-12 - 新規作成

 

Programming Items トップページ

プライバシーポリシー