_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
进入 vector 。但是 vector 的类型是 <gcl::SectionConnectionT*>
一些谷歌搜索把我带到了 transform_iterator
我无法理解如何理解。我是这样用的。但得到编译错误S
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
未对可写左值迭代器概念建模,因为需要将其用于 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));
关于c++ - 使用 boost transform_iterator 有条件地进行 std::transform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12027951/