C++ boost::filter_迭代器也为end()调用谓词?

C++ boost::filter_迭代器也为end()调用谓词?,c++,boost,foreach,filter-iterator,C++,Boost,Foreach,Filter Iterator,我正在使用boost::filter\u迭代器。我使用它来创建一个跳过某些元素的自定义迭代器。这些元素是一些粒子之间的接触,一些接触定义了它们的几何体(Contact::geom);我想跳过那些没有定义它的人(即!Contact::geom)。缩短的版本如下所示(我可能无法将其缩短): struct联系人{ 共享_ptr geom; bool isReal(){return(bool)geom;} /*其他一些数据*/ }; 结构ContactContainer{ //这是底层容器 typede

我正在使用boost::filter\u迭代器。我使用它来创建一个跳过某些元素的自定义迭代器。这些元素是一些粒子之间的接触,一些接触定义了它们的几何体(Contact::geom);我想跳过那些没有定义它的人(即!Contact::geom)。缩短的版本如下所示(我可能无法将其缩短):

struct联系人{
共享_ptr geom;
bool isReal(){return(bool)geom;}
/*其他一些数据*/
};
结构ContactContainer{
//这是底层容器
typedef向量ContainerT;
ContainerT数据;
//定义是否跳过此联系人的谓词
结构以色列{
bool操作符()(shared_ptr&c){return c&&c->isReal();}
bool操作符()(const shared_ptr&c){return c&&c->isReal();}
};
typedef boost::filter\u迭代器迭代器;
typedef boost::filter_iterator const_iterator;
//返回代理迭代器
迭代器begin(){返回数据。begin();}
迭代器end(){return data.end();}
常量迭代器begin()常量{返回常量迭代器(data.begin());}
const_迭代器end()const{return const_迭代器(data.end());}
size_t size()常量{return data.size();}
};
现在在代码的一个地方,我使用BOOST_FOREACH迭代ContactContainer,它应该过滤掉联系人!c->isReal()自动:
BOOST_FOREACH(常数共享\u ptr&c、联系人){
/*在这里做点什么*/
};
我一直从调试器获得以下信息:

#4  <signal handler called>
#5  0x00007fc94cdc4ad4 in boost::shared_ptr<CGeom>::operator CGeom* boost::shared_ptr<CGeom>::* (this=0x2dee04a518aa88) at /usr/include/boost/smart_ptr/detail/operator_bool.hpp:47
#6  0x00007fc94cdbefe4 in Contact::isReal (this=0x2dee04a518aa80) at /home/eudoxos/yade/build-tr2/dbg/include/yade/pkg/dem/Particle.hpp:101
#7  0x00007fc94ce2a2da in ContactContainer::IsReal::operator() (this=0x7fff1ebe1038, c=...) at pkg/dem/ContactContainer.cpp:8
#8  0x00007fc94d0a9e05 in boost::filter_iterator<ContactContainer::IsReal, __gnu_cxx::__normal_iterator<boost::shared_ptr<Contact>*, std::vector<boost::shared_ptr<Contact>, std::allocator<boost::shared_ptr<Contact> > > > >::satisfy_predicate (this=0x7fff1ebe1030) at /usr/include/boost/iterator/filter_iterator.hpp:100
#9  0x00007fc94d0a5ae7 in boost::filter_iterator<ContactContainer::IsReal, __gnu_cxx::__normal_iterator<boost::shared_ptr<Contact>*, std::vector<boost::shared_ptr<Contact>, std::allocator<boost::shared_ptr<Contact> > > > >::filter_iterator (this=0x7fff1ebe1030, x=..., end_=...) at /usr/include/boost/iterator/filter_iterator.hpp:72
#10 0x00007fc94d09b311 in ContactContainer::end (this=0x3df5b40) at /home/eudoxos/yade/build-tr2/dbg/include/yade/pkg/dem/ContactContainer.hpp:46
#11 0x00007fc94d0b098b in boost::range_detail::range_end<ContactContainer> (c=...) at /usr/include/boost/range/end.hpp:50
#12 0x00007fc94d0ace87 in boost::end<ContactContainer> (r=...) at /usr/include/boost/range/end.hpp:99
#13 0x00007fc94d0a95c1 in boost::foreach_detail_::end<ContactContainer, mpl_::bool_<false> > (col=...) at /usr/include/boost/foreach.hpp:693
#4
#5 0x00007fc94cdc4ad4位于/usr/include/boost/smart\u ptr/detail/operator\u bool.hpp:47处的boost::shared\u ptr::operator CGeom*boost::shared\u ptr::*(此参数=0x2dee04a518aa88)
#6 0x00007fc94cdbefe4联系方式::isReal(此=0x2dee04a518aa80)位于/home/eudoxos/yade/build-tr2/dbg/include/yade/pkg/dem/Particle.hpp:101
#7在pkg/dem/ContactContainer.cpp:8处的ContactContainer::IsReal::operator()中的0x00007fc94ce2a2da(this=0x7fff1ebe1038,c=…)
#8/usr/include/boost/iterator/filter\u iterator::success\u谓词(this=0x7fff1ebe1030)中的0x00007fc94d0a9e05。hpp:100
#9在/usr/include/boost/iterator/filter\u iterator处的boost::filter\u iterator::filter\u iterator(this=0x7fff1ebe1030,x=…,end\u=…)中的0x00007fc94d0a5ae7。hpp:72
#ContactContainer中的10 0x00007fc94d09b311::end(this=0x3df5b40)at/home/eudoxos/yade/build-tr2/dbg/include/yade/pkg/dem/ContactContainer.hpp:46
#11 0x00007fc94d0b098b在/usr/include/boost/range/end处的boost::range_detail::range_end(c=…)中。hpp:50
#12 0x00007fc94d0ace87位于/usr/include/boost/range/end的boost::end(r=…)中。hpp:99
#13 0x00007fc94d0a95c1位于/usr/include/boost/foreach.hpp:693处的boost::foreach_detail_::end(col=…)中
如果我读对了,过滤器迭代器也会调用ContactContainer::end()上的谓词,这实际上是ContactContainer::data.end()。这是预期的行为吗?不幸的是,文档只显示数字过滤,我不知道如何继续

有人能启发我吗

干杯


Vaclav

过滤器迭代器的构造函数使用两个迭代器:开始和结束,如中所示

filter_iterator(Iterator x, Iterator end = Iterator());
以下更改使您的示例为我执行:

iterator begin(){ return iterator(data.begin(), data.end()); }
iterator end(){ return iterator(data.end(), data.end());}

测试运行:

跳过是如何完成的?是否存在跳过end()或end()之前的值的风险?谢谢,它可以工作!(不是责怪boost,但我想知道为什么它的文档有时如此难以阅读)
iterator begin(){ return iterator(data.begin(), data.end()); }
iterator end(){ return iterator(data.end(), data.end());}