2012-02-15 2 views
3

범위 시퀀스에 적용해야하는 복잡한 알고리즘 목록을 작성하려고합니다. 다음 코드와 비슷한 구문을 사용하여 여러 알고리즘을 중첩시키고 싶습니다. 내 유일한 문제는 컴파일되지 않는다는 것입니다. 어떤 제안?부스트 범위 알고리즘 결합, 예. boost :: copy and 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

@ 애드 리안 :이 답변으로 문제가 해결되면 옆에있는 체크 표시를 클릭하여 문제를 수락하는 것이 좋습니다. :) – Xeo

+0

첫 번째 변환은 푸시 백 시퀀스가 ​​아닌 벡터의 범위 삽입 멤버를 호출하기 위해 짧아 질뿐만 아니라 boost :: push_back (y, x | filtered (pred));에 최적화 될 수 있습니다. – TemplateRex