结合了boost范围算法,例如boost::copy和boost::remove_if
Combining boost range algorithms, eg. 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:devthirdpartyboostboost-1.48.0-windows-vc90-x32-p64925installedincludeboost-1_48boost/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:devthirdpartyboostboost-1.48.0-windows-vc90-x32-p64925installedincludeboost-1_48boost/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:devthirdpartyboostboost-1.48.0-windows-vc90-x32-p64925installedincludeboost-1_48boost/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:devthirdpartyboostboost-1.48.0-windows-vc90-x32-p64925installedincludeboost-1_48boost/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:devthirdpartyboostboost-1.48.0-windows-vc90-x32-p64925installedincludeboost-1_48boost/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> ..........sourceyottalibrariessnlunittestsStackOverflow.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
....
问题是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
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 使用函数"remove"删除重复元素
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- Boost Spirit,获取迭代器内部语义动作
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 链表的泛型函数remove()与成员函数remove)
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 如何确保 boost::文件系统::remove 不会尝试删除由其他进程使用的文件