2012-02-15 25 views
3

レンジシーケンスに適用する必要があるアルゴリズムの複雑なリストを作成しようとしています。次のコードに似た構文を使用して、多くのアルゴリズムをネストしたいと思います。私の唯一の問題は、それがコンパイルされないことです。助言がありますか?ブースト範囲アルゴリズムを組み合わせる。 boost :: copyとboost :: remove_if

bool pred(double x); 

double d[]={1,2,3,4}; 
std::vector<double> x(d,d+4); 
std::vector<double> y; 
boost::copy(x, std::back_inserter(y)); // OK 
boost::copy(boost::remove_if(x,&pred), std::back_inserter(y)); // ERROR 

この構文を使用するには、内部アルゴリズムのテンプレートパラメータを指定する必要はありません。これはautoキーワードを使用することで簡略化できますが、コードの後方互換性を保つ必要があります。

これは、エラーメッセージの断片である:

1>c:\dev\thirdparty\boost\boost-1.48.0-windows-vc90-x32-p64925\installed\include\boost-1_48\boost/range/iterator.hpp(63) : error C2039: 'type' : is not a member of 'boost::mpl::eval_if_c<C,F1,F2>' 
1>  with 
1>  [ 
1>   C=true, 
1>   F1=boost::range_const_iterator<std::_Vector_iterator<double,std::allocator<double>>>, 
1>   F2=boost::range_mutable_iterator<const std::_Vector_iterator<double,std::allocator<double>>> 
1>  ] 

1>  c:\dev\thirdparty\boost\boost-1.48.0-windows-vc90-x32-p64925\installed\include\boost-1_48\boost/range/concepts.hpp(256) : see reference to class template instantiation 'boost::range_iterator<C>' being compiled 
1>  with 
1>  [ 
1>   C=const std::_Vector_iterator<double,std::allocator<double>> 
1>  ] 
1>  c:\dev\thirdparty\boost\boost-1.48.0-windows-vc90-x32-p64925\installed\include\boost-1_48\boost/concept/detail/has_constraints.hpp(42) : see reference to class template instantiation 'boost::SinglePassRangeConcept<T>' being compiled 
1>  with 
1>  [ 
1>   T=const std::_Vector_iterator<double,std::allocator<double>> 
1>  ] 
1>  c:\dev\thirdparty\boost\boost-1.48.0-windows-vc90-x32-p64925\installed\include\boost-1_48\boost/concept/detail/msvc.hpp(53) : see reference to class template instantiation 'boost::concepts::not_satisfied<Model>' being compiled 
1>  with 
1>  [ 
1>   Model=boost::SinglePassRangeConcept<const std::_Vector_iterator<double,std::allocator<double>>> 
1>  ] 
1>  c:\dev\thirdparty\boost\boost-1.48.0-windows-vc90-x32-p64925\installed\include\boost-1_48\boost/range/algorithm/copy.hpp(33) : see reference to class template instantiation 'boost::concepts::require<Model>' being compiled 
1>  with 
1>  [ 
1>   Model=boost::SinglePassRangeConcept<const std::_Vector_iterator<double,std::allocator<double>>> 
1>  ] 
1>  ..\..\..\..\..\source\yotta\libraries\snl\unittests\StackOverflow.cpp(7) : see reference to function template instantiation 'OutputIterator boost::range::copy<std::_Vector_iterator<_Ty,_Alloc>,std::back_insert_iterator<_Container>>(const SinglePassRange &,OutputIterator)' being compiled 
1>  with 
.... 
+0

は範囲が参照によって渡される必要がありますか?または範囲への参照を含むクラスにすることはできますか? – Adrian

+0

+1「私の唯一の問題はコンパイルされないということです」 'cout << the_answer_to_it_all();の唯一の問題は、コンパイルできないということです。 –

答えて

6

問題は、あなたがいない場合remove_if(およびそのことについては、すべてのアルゴリズムは、)、単一イテレータ、ない範囲を返すということです返すべき範囲の型を指定します(あなたは望みそうにありません)。

簡単な修正はthe adaptor filteredを使用することです:

#include <boost/range/adaptor/filtered.hpp> 
#include <boost/range/algorithm/copy.hpp> 
#include <iterator> 
#include <vector> 
#include <iostream> 

bool pred(double x){ 
    return x > 2.0; 
} 

int main(){ 

    double d[]={1,2,3,4}; 
    std::vector<double> x(d,d+4); 
    std::vector<double> y; 
    boost::copy(x | boost::adaptors::filtered(pred), std::back_inserter(y)); 
    boost::copy(y, std::ostream_iterator<double>(std::cout, " ")); 
} 

出力:3 4

+0

ありがとうございます。今私は問題を見る。 – Adrian

+1

@Adrian:この回答があなたの問題を解決した場合は、その横のチェックボックスをクリックして問題を受け入れることを検討してください。 :) – Xeo

+0

最初の変換は、boost :: push_back(y、x | filtered(pred));に最適化できます。これは型を短くするだけでなく、プッシュバックのシーケンスではなくベクトルのrange-insertメンバを呼び出します。 – TemplateRex

関連する問題