如何使用std::lower_bound比较对象变量,而不使用第二个对象进行比较
How to use std::lower_bound to compare object variables without second object to compare with
我想将我的"WorldChunk"类函数"getX(("answers"getY(("与传递给函数的"chunk_x"answers"chunk_y"进行比较,但我不想创建新的"WorldChunk"实例进行比较。
我试过那样的东西,但不起作用。
int ChunkGrid::unload_chunk(unsigned int chunk_x, unsigned int chunk_y)
{
auto chunk = std::lower_bound(loaded_chunks.begin(), loaded_chunks.end(), NULL,
[chunk_x, chunk_y](const WorldChunk& ch, const auto * null)
{
return (ch.getX() == chunk_x && ch.getY() == chunk_y) ? true : false;
});;
//rest of the function
}
错误日志:
Error C2672 'operator __surrogate_func': no matching overloaded function found.
Error C2784 'auto ChunkGrid::unload_chunk::<lambda_d0b216222e2c66d42cf1e3316f6d68ac>::operator ()(const WorldChunk &,const _T1 *) const': could not deduce template argument for 'const _T1 *' from 'const _Ty'
您遇到的问题是,您试图通过lambda捕获而不是参数传递比较值。只要做得正确;不要求第三个参数的类型与迭代器的CCD_ 1的类型相同:
int ChunkGrid::unload_chunk(vec2<unsigned int> chunk_loc)
{
auto chunk = std::lower_bound(loaded_chunks.begin(), loaded_chunks.end(), chunk_loc,
[](const WorldChunk& ch, const vec2<unsigned int> &loc)
{
return (ch.getX() < loc.x && ch.getY() < loc.y) ? true : false;
});;
//rest of the function
}
这里真正的问题是lower_bound
不是一个通用的搜索函数(即std::find
(。它要求相对于搜索函数对loaded_chunk
序列进行排序(或至少根据测试值进行分区(。也就是说,与值的比较为true的所有元素必须位于与值的对比为false的所有元素之前。因此,除非你按X/Y位置对这个块列表进行排序(按照确切的顺序;X<X,然后Y<Y(,否则这是行不通的。
相关文章:
- 对于BTreeMap和其他依赖于Ord的东西,是否有等效于C++比较器对象?
- C++ <algorithm> 使用对象作为比较定义的 sort()
- C++,如何使用常量对象和非常量对象进行比较?
- 绘制一个对象,比较模具缓冲区的两个不同值
- 隐式转换为比较函数对象(函子)用于 std::sort 而不是 std::map?
- 为什么地图需要实现'operator<'以及如何比较对象?
- 如何使用std::lower_bound比较对象变量,而不使用第二个对象进行比较
- 可作为常量调用的比较对象
- 如何为C++映射创建自己的字符串比较对象
- 如何用类别的consumetrized构造函数声明使用类的比较对象设置的STL设置
- 我可以防止 std::sort 复制传递的比较对象吗?
- 是否可以创建一个函数来比较对象向量中动态选择的变量
- 比较对象的3个数字属性
- 如何将比较对象或函数传递给类函数
- C++STL priority_queue,比较对象的方法
- 比较对象标识符与字符串
- c++ STL集合:比较对象与外部状态
- 比较对象和指针
- 比较对象的两个向量的元素
- c++比较对象