c++ - 使用 boost transform_iterator 有条件地进行 std::transform

标签 c++ boost iterator

_pimpl->_connectionsstd::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/

相关文章:

c++ - 我可以在统一包装器中调用包含 block 的 iOS 代码吗?

c++ - 使用 boost::filesystem 对目录的子文件夹和文件名进行数字排序

c++ - 实现 cpp const 迭代器会崩溃并烧毁

c++ - 如何计算没有标签信息的 mp3 文件的哈希值?

c++ - 应用了非限定名称查找而不是依赖于参数的名称查找

boost - boost::multi_array 的内存大小

c++ - 有什么方法可以结合 std::istream_iterator 和 std::for_each_n() 吗?

java - ListIterator 和并发修改异常的问题

c++ - 带循环检测的 Callgrind 性能分析

c++ - 在构造函数中 boost enable_if