zip_iterator and 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 [下限]/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 的编译问题。
相关文章:
- 空集"Out of bound iterator"
- 将张量的"lower diagonal"映射到矩阵,作为将矩阵的下三角形部分提取到向量的推广
- 为什么<<低;和 cout<<v.begin;单独给出错误,但 cout <<(lower.begin());没有给出任何错误,并使代码易于编译
- 使用多个文件时"array bound is not an integer constant before ']' token"
- "A reference may be bound only to an object",为什么"const int &ref = 3;"有效?
- 如何修复"reference cannot be bound to dereferenced null pointer"警告
- 当你有一个像 ::lower 这样的参数时,这在C++中是什么意思
- GCC 检测"subindex out of bound"错误
- 如果我没有得到分段错误,如何找到"index out of bound"
- 一个程序将字符串Upper char更改为Lower char得到错误
- std:: lower的行为在不同的语言环境中是如何变化的?
- 在main中声明Array之前,Array bound不是整型常量
- 获取错误"array bound is not an integer constant before ']' token"
- 如何在C或c++中获得与Java中的toLowerCase或Python中的string.lower()相同的结果
- 在运行时创建结构时"array bound is not an integer constant"