2009-08-24 11 views
2

私はこのようになりますいくつかのコードがあります。Boost :: tupleはPythonのitemgetterと同じですか?

typedef tuple<int, int, double> DataPoint; 
vector<DataPoint> data; 
vector<double> third_column_only; 
// Code to read in data goes here. 
transform(data.begin(), data.end(), back_inserter(values), tuples::get<1, DataPoint>); 

を残念ながら、最後の行はコンパイルされません - それは私にこのようなメッセージ与えます:Pythonの演算子を使用するように、基本的に

 
/path/to/boost/tuple/detail/tuple_basic.hpp: In instantiation of `boost::tuples::cons': 
/path/to/boost/tuple/detail/tuple_basic.hpp:144: instantiated from `boost::tuples::element >' 
program.cc:33: instantiated from here 
/path/to/boost/tuple/detail/tuple_basic.hpp:329: error: `boost::tuples::cons::tail' has incomplete type 
/path/to/boost/tuple/detail/tuple_basic.hpp:329: error: invalid use of template type parameter 
/path/to/boost/tuple/detail/tuple_basic.hpp:151: confused by earlier errors, bailing out 

を。 itemgetter関数のように、私は次のようにしたい:

transform(data.begin(), data.end(), back_inserter(values), itemgetter(2)) 

私はどのようにしてBoostを使ってそれを行うことができますか?

答えて

0

これを行うには、boost::bindが必要です。

double get2(boost::tuple<int,int,double> const& v) { 
    return boost::get<2>(v) ; 
} 

int main() { 
    using namespace std ; 
    using namespace boost ; 
    typedef tuple<int, int, double> DataPoint; 
    vector<DataPoint> data; 
    vector<double> third_column_only; 
    // Code to read in data goes here. 
    transform(data.begin(), data.end(), back_inserter(values), bind(get2, _1); 
} 

Acturallyは、get2機能も不要であるが、我々はpossibllyのように、boost::get機能のために、正確にテンプレート引数を指定する必要があります。

transform(data.begin(), data.end(), back_inserter(values), 
      bind(get<2, int, tuple<int, double> >, _1); 

それは私がどのようにしないことを残念ですテンプレート引数を指定するので、ヘルパー関数を使用しました。申し訳ありません。

+0

Thanks-boost :: bindはget <2、int、tupleのテンプレート宣言と同様に私が探していたものでした> - getのテンプレート宣言が代わりにを取得します。 しかし、bindを使用しても、get2のような関数宣言を追加しなければ、コンパイルできないようです。私はcygwinの下でgcc 3.4を使用していますので、コンパイラのバージョンの問題かもしれません - まだ使用しています bind(get <2、int、tuple >、_1) 私は " unknown型の最初の引数でバインドする不明な呼び出し " –

関連する問題