繁体   English   中英

zip_iterator和lower_bound

[英]zip_iterator and lower_bound

我无法弄清楚如何使用zip_iterator调用lower_bound

这不会编译:

#include <boost/iterator/zip_iterator.hpp>
#include <vector>
#include <algorithm>

void main()
{
    typedef int Key;
    typedef double Value;

    typedef boost::tuple<typename std::vector<Key>::iterator,
                         typename std::vector<Value>::iterator> the_iterator_tuple;
    typedef boost::zip_iterator<the_iterator_tuple> the_zip_iterator;

    std::vector<Key>   keys_;
    std::vector<Value> values_;

    // Add values to keys_ and values_...

    auto it = std::lower_bound(
        the_zip_iterator(the_iterator_tuple(keys_.begin(), values_.begin())),
        the_zip_iterator(the_iterator_tuple(keys_.end(), values_.end())),
        123,
        [](const the_iterator_tuple & it, const int v) -> bool { return *boost::get<0>(it) < v; }
    );

    // Use "it"...
}

VS2010说它“无法将参数1从'int'转换为'const std :: _ Vector_iterator <_Myvec>&'”(对于同样的错误还有几十个其他的东西),但它与一个不起眼的boost :: tuple构造函数有关,而不是给定的lambda。

我究竟做错了什么 ?

这看起来像VS2010中的“概念检查”错误。

25.4.3.1 [lower.bound] / p1:

要求:: [first,last)的元素e应根据表达式e < valuecomp(e, value)进行分区。

即只有*it < v是必需的。

upper_bound算法具有相反的要求: v < *it equal_range要求两个表达式都能正常工作。

std::lower_bound(it, end, v)需要能够同时执行*it < vv < *it 您的函数对象仅支持其中一个。

由于对此有评论,请留下以上陈述:事实并非如此。 正如霍华德指出的那样,比较需要使用comp(*it, v) ,即,不需要对称操作。

但是,查看boost::zip_iterator<It0, It1>的文档,似乎*it产生一个boost::tuple<typename It0::reference, typename It1::reference> 因此,添加typedef

typedef boost::tuple<typename std::vector<Key>::reference,
                     typename std::vector<Value>::reference> the_reference_tuple;

......并改变lambda成为

[](the_reference_tuple const& it, int v) { return it.get<0>() < v; }

使用gcc和clang解决编译问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM