结合boost范围算法,例如boost::copy和boost::remove_if。

3

我正在尝试编写一系列复杂的算法,必须应用于一个范围序列。我想使用类似以下代码的语法嵌套许多算法。我的唯一问题是它无法编译。有什么建议吗?

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   
....

这些范围必须通过引用传递吗?还是它们可以是一个包含对范围引用的类? - Adrian
“我的唯一问题是它无法编译。”对于“cout << the_answer_to_it_all();”的唯一问题就是它无法编译。 - Phil H
1个回答

7
问题在于,remove_if(以及所有算法)返回一个单一的迭代器,而不是一个范围,如果您没有指定应该返回的范围类型(您似乎并不想要这个),这就是问题所在。
一个简单的解决方法是使用适配器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

1
@Adrian:如果这个答案解决了你的问题,你可以考虑点击旁边的勾号接受它。 :) - Xeo
第一个转换可以优化为 boost::push_back(y, x | filtered(pred));,这不仅打字更短,而且调用了向量的范围插入成员,而不是一系列的 push back。 - TemplateRex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接