upper_bound和lower_bound不一致的值要求
upper_bound and lower_bound inconsistent value requirements
我看到了 std::lower_bound(( 和 std::upper_bound(( 语法中的不一致(嗯,类型转换,真的(,想知道是否有人可以阐明?根据评论,尽管第 2 行与第 1 行明显相似,但它将无法编译;您需要使用第 3 行中显示的表单(至少在 GCC 4.7.3/Ubuntu 64 位上 - 这就是我要玩的全部内容(
#include <set>
#include <algorithm>
using namespace std;
class MyInt {
private:
int val;
public:
MyInt(int _val): val(_val) {}
bool operator<(const MyInt& other) const {return val < other.val;}
};
int main() {
set<MyInt> s;
s.insert(1); // demonstrate implicit conversion works
s.insert(MyInt(2));
s.insert(3); // one last one for the road
set<MyInt>::iterator itL = lower_bound(s.begin(), s.end(), 2); //LINE 1
// the line below will NOT compile
set<MyInt>::iterator itU = upper_bound(s.begin(), s.end(), 2); //LINE 2
// the line below WILL compile
set<MyInt>::iterator itU2 = upper_bound(s.begin(), s.end(), MyInt(2)); // LINE 3
return 0;
}
我不认为这是一个错误。如果你看一下std::upper_bound
的(可能的(实现,比较是这样的
if (!(value < *it)) { ... } // upper_bound, implicit conversion `MyInt`->`int` doesn't work
并且由于operator<
是MyInt
的成员函数(而不是int
的成员函数,它不是类类型(,因此代码无法编译,因为没有从MyInt
到int
的转换。另一方面,在std::lower_bound
中,*it
出现在比较的 lhs 上,并且value
(类型 int
(在传递给 MyInt::operator<
时可以隐式转换为 MyInt
。
if (*it < value) { ... } // lower_bound, implicit conversion `int`->`MyInt` works
这就是为什么最好将比较运算符实现为非成员的原因,这样您就不会有这种不对称性。Scott Meyers 的《有效C++》一书中也提到了这一点:第 24 项:当类型转换应应用于所有参数时声明非成员函数。
MyInt::operator int(){return val;}
,用于隐式转换MyInt
到int
set<MyInt>::iterator itU = upper_bound(s.begin(), s.end(), MyInt(2));
相反。
相关文章:
- 空集"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"