我看到了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 …
Run Code Online (Sandbox Code Playgroud)