2012-02-15 57 views
3

我想寫一個複雜的算法列表,必須應用於範圍序列。我想使用類似於以下代碼的語法來嵌套許多算法。我唯一的問題是它不會編譯。有什麼建議麼?結合增強範圍算法,例如。升壓::複製和提升:: 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 2012-02-15 12:14:44

+0

+1「我唯一的問題是它不會編譯」。 'cout << the_answer_to_it_all();'唯一的問題是不能編譯... – 2012-06-19 11:09:26

回答

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 2012-02-15 12:49:58

+1

@Adrian:如果這個答案解決了您的問題,您可能需要考慮通過點擊旁邊的勾號來接受它。 :) – Xeo 2012-02-22 11:33:46

+0

第一個轉換可以優化爲'boost :: push_back(y,x | filtered(pred));'它不僅更短,而且調用向量的範圍插入成員,而不是一系列推回。 – TemplateRex 2014-02-14 17:50:41