[英]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 < value
或comp(e, value)
進行分區。
即只有*it < v
是必需的。
upper_bound
算法具有相反的要求: v < *it
。 而equal_range
要求兩個表達式都能正常工作。
std::lower_bound(it, end, v)
需要能夠同時執行*it < v
和v < *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.