小编aho*_*aho的帖子

upper_bound和lower_bound不一致的值要求

我看到了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)

c++ stl lower-bound implicit-conversion upperbound

5
推荐指数
1
解决办法
312
查看次数

标签 统计

c++ ×1

implicit-conversion ×1

lower-bound ×1

stl ×1

upperbound ×1