带有==操作符的struct vector的上界
C++ upper_bound on struct vector with == operator
我有以下结构体,
struct cube
{
int index l , b , h;
bool operator<(const cube & c2) const
{
if (l == c2.l && b == c2.b && h == c2.h)
return index < c2.index;
if (l == c2.l && b == c2.b)
return h < c2.h;
if (l == c2.l )
return b < c2.b;
return l < c2.l;
}
bool operator==(const cube c2)
{
return index != c2.index && l == c2.l && b == c2.b;
}
};
现在我想在这个结构体的vector上应用upper_bound作为==操作符中的条件。
然而,它仍然返回那些索引相同的迭代器
int pos2 = upper_bound(v.begin(),v.end(),v[i]) - v.begin();
。e v[我]。Index = v[pos2].index
对于两个cube
实例foo
和bar
,当foo == bar
也是true
时,foo < bar
可能是true
。您可以通过将index == c2.index && l == c2.l && b == c2.b
写入operator==
中的返回表达式来修复此问题。
这个矛盾是你的问题的根本原因,尽管注意std::upper_bound
本身只要求operator<
被适当地实现;
难道index
不是cube
s的集合而不是给定的cube
的属性吗?也就是说,它不应该出现在cube
类?
相关文章:
- 附加使用 Struct 作为"multikey"并将 std::vector 用作映射值的映射
- 在c++Primer中使用Struct和Vector时出现错误C2661
- OCCI - setDataBuffer + vector<struct>
- C 如何推动vector struct的成员
- 使用 boost.python 公开 std::vector<struct>
- 使用Isringstream和struct/vector在给定的数据之间建立连接
- 当编译器看到 std::vector<Typo> 和 std::vector<struct 拼写错误时发出的诊断之间的差异背后的理由是什么>
- 使用 std::transform 将 std::vector<struct{key; val;}> 转换为 std::map<key, val>
- 将数据从struct二进制复制到std::vector
- 元素 std::vector <struct>的更有成效的段落
- Dynamic vector using Struct
- 3D Vector of struct emplace_back
- struct vs vector作为数据持有者
- C++错误: map.insert(make_pair(struct, vector<struct>));
- 为 std::vector <struct>创建一个 getter 函数
- 创建一个Struct,然后通过引用传递一个带有这些Struct的vector
- 带有==操作符的struct vector的上界
- Vector of struct (copy_n和归并排序)
- 将vector(在struct中定义时)传递给函数
- 使用Struct类型的Vector迭代器出错