[英]Using `std::equal_range` with `boost::transform_iterator`
[英]using boost transform_iterator to std::transform conditionally
_pimpl->_connections
是一个std::map
因此它的元素是std::pair<KeyType, gcl::SectionConnectionT*>
我想使用谓词gcl::SectionConnectionT::NotConnectedTo(r)
过滤掉不必要的值。 但是如果我使用
std::remove_copy_if(it_pair.first, it_pair.second, std::back_inserter(sectionConnections), gcl::SectionConnectionT::NotConnectedTo(r));
它试图将插入pair
到载体中。 但是向量的类型为<gcl::SectionConnectionT*>
一些Google搜索将我带到了transform_iterator
,我无法理解该如何理解。 我是这样用的 但是出现编译错误
std::pair<CollectionT::iterator, CollectionT::iterator> it_pair = _pimpl->_connections.equal_range(l);
std::vector<gcl::SectionConnectionT*> sectionConnections;
std::remove_copy_if(it_pair.first, it_pair.second, boost::make_transform_iterator(std::back_inserter(sectionConnections), util::shorthand::pair_second()), gcl::SectionConnectionT::NotConnectedTo(r));
Boost.Iterator库可能无法推断until::shorthand::pair_second::operator()(CollectionT::value_type&) const
的返回类型,因此返回的transform_iterator
并未按照要求对Writable Lvalue Iterator概念进行建模。用于std::remove_copy_if
(输出迭代器)的第三个参数。
但是,以下工作原理:
//#include <boost/lambda/bind.hpp>
//#include <boost/lambda/lambda.hpp>
//#include <boost/range/adaptor/map.hpp>
//#include <boost/range/algorithm/remove_copy_if.hpp>
//namespace gcl {
//struct SectionConnectionT {
// bool NotConnectedTo(RType r) const;
// // ...
//};
//}
boost::remove_copy_if(it_pair | boost::adaptors::map_values,
std::back_inserter(sectionConnections),
boost::lambda::bind(&gcl::SectionConnectionT::NotConnectedTo, boost::lambda::_1, r));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.