upper_bound()比较函数的定义方法

qqwqert001 2013-07-26 01:11:55
我定义了一个vector<int>ar,ar中存有3个元素:0,1,2。另外还有一个数组l[3]={0,10,20}。
若给定一个数x,此时我想要利用upper_bound()函数查找ar中的第一个满足l[a]>x的元素a。
那么函数upper_bound(ar.begin(),ar.end(),x,cmp())中的第四个参数cmp()该怎么定义呢?
...全文
1042 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohuh421 2013-07-26
  • 打赏
  • 举报
回复
upper_bound的第四个参数是一个仿函数. 你可以这样写一个类. class isBiger { private: int m_i[3]; //你需要比较的数组I[3]={0,10,20} public: void setI(int i[3]) { .... } BOOL operator()(int val1, int val2) { if( m_i[val1]>val2) return true; else return false; } }; int ValArry[3] = {0,10,20}; isBiger big; big.Set(ValArry); upper_bound(ar.begin(),ar.end(),x, big);
qqwqert001 2013-07-26
  • 打赏
  • 举报
回复
感谢两位的回答,谢谢!
qqwqert001 2013-07-26
  • 打赏
  • 举报
回复
引用 20 楼 rocktyt2 的回复:
[quote=引用 18 楼 qqwqert001 的回复:] 我按照上面说的方法做了一下测试,发现upper_bound还是没能得出正确的结果。。 我测试了lower_bound(,,2,cmp_lower())与upper_bound(,,1,cmp_upper()) 按理说两者应该等价,可结果前者结果为1,正确。后者结果为3,直接找到ar.end()了。。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int l[3]={0,10,20};
vector<int>ar;
struct cmp_lower//lower_bound
{
    bool operator  () (int index,int x)
    {
        return l[index]<x;
    }
};
struct cmp_upper//upper_bound
{
    bool operator  () (int x,int index)
    {
        return l[index]<x;
    }
};

int main ()
{
    for(int i=0;i<3;i++)
        ar.push_back(i);
    int x;
    while(cin>>x)
    {
        int lower_result=lower_bound(ar.begin(),ar.end(),x+1,cmp_lower())-ar.begin();
        int upper_result=upper_bound(ar.begin(),ar.end(),x,cmp_upper())-ar.begin();
        printf("lower_result:%d  upper_result:%d\n",lower_result,upper_result);
    }
    return 0;
}

cmp_upper里比较的方向反了,小于改成大于或者两边倒一下吧 你要的是x比index小[/quote]
引用 20 楼 rocktyt2 的回复:
[quote=引用 18 楼 qqwqert001 的回复:] 我按照上面说的方法做了一下测试,发现upper_bound还是没能得出正确的结果。。 我测试了lower_bound(,,2,cmp_lower())与upper_bound(,,1,cmp_upper()) 按理说两者应该等价,可结果前者结果为1,正确。后者结果为3,直接找到ar.end()了。。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int l[3]={0,10,20};
vector<int>ar;
struct cmp_lower//lower_bound
{
    bool operator  () (int index,int x)
    {
        return l[index]<x;
    }
};
struct cmp_upper//upper_bound
{
    bool operator  () (int x,int index)
    {
        return l[index]<x;
    }
};

int main ()
{
    for(int i=0;i<3;i++)
        ar.push_back(i);
    int x;
    while(cin>>x)
    {
        int lower_result=lower_bound(ar.begin(),ar.end(),x+1,cmp_lower())-ar.begin();
        int upper_result=upper_bound(ar.begin(),ar.end(),x,cmp_upper())-ar.begin();
        printf("lower_result:%d  upper_result:%d\n",lower_result,upper_result);
    }
    return 0;
}

cmp_upper里比较的方向反了,小于改成大于或者两边倒一下吧 你要的是x比index小[/quote] 原来如此,我终于明白了。。
qqwqert001 2013-07-26
  • 打赏
  • 举报
回复
引用 19 楼 zyaiwx 的回复:
看不懂题目,怎么办
比如说x=5,那么a应该为1,因为l[1]=10>5,l[0]=0<5. 故a=1符合要求
rocktyt 2013-07-26
  • 打赏
  • 举报
回复
引用 18 楼 qqwqert001 的回复:
我按照上面说的方法做了一下测试,发现upper_bound还是没能得出正确的结果。。 我测试了lower_bound(,,2,cmp_lower())与upper_bound(,,1,cmp_upper()) 按理说两者应该等价,可结果前者结果为1,正确。后者结果为3,直接找到ar.end()了。。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int l[3]={0,10,20};
vector<int>ar;
struct cmp_lower//lower_bound
{
    bool operator  () (int index,int x)
    {
        return l[index]<x;
    }
};
struct cmp_upper//upper_bound
{
    bool operator  () (int x,int index)
    {
        return l[index]<x;
    }
};

int main ()
{
    for(int i=0;i<3;i++)
        ar.push_back(i);
    int x;
    while(cin>>x)
    {
        int lower_result=lower_bound(ar.begin(),ar.end(),x+1,cmp_lower())-ar.begin();
        int upper_result=upper_bound(ar.begin(),ar.end(),x,cmp_upper())-ar.begin();
        printf("lower_result:%d  upper_result:%d\n",lower_result,upper_result);
    }
    return 0;
}

cmp_upper里比较的方向反了,小于改成大于或者两边倒一下吧 你要的是x比index小
zybjtu 2013-07-26
  • 打赏
  • 举报
回复
看不懂题目,怎么办
qqwqert001 2013-07-26
  • 打赏
  • 举报
回复
我按照上面说的方法做了一下测试,发现upper_bound还是没能得出正确的结果。。 我测试了lower_bound(,,2,cmp_lower())与upper_bound(,,1,cmp_upper()) 按理说两者应该等价,可结果前者结果为1,正确。后者结果为3,直接找到ar.end()了。。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int l[3]={0,10,20};
vector<int>ar;
struct cmp_lower//lower_bound
{
    bool operator  () (int index,int x)
    {
        return l[index]<x;
    }
};
struct cmp_upper//upper_bound
{
    bool operator  () (int x,int index)
    {
        return l[index]<x;
    }
};

int main ()
{
    for(int i=0;i<3;i++)
        ar.push_back(i);
    int x;
    while(cin>>x)
    {
        int lower_result=lower_bound(ar.begin(),ar.end(),x+1,cmp_lower())-ar.begin();
        int upper_result=upper_bound(ar.begin(),ar.end(),x,cmp_upper())-ar.begin();
        printf("lower_result:%d  upper_result:%d\n",lower_result,upper_result);
    }
    return 0;
}

ri_aje 2013-07-26
  • 打赏
  • 举报
回复
引用 14 楼 qqwqert001 的回复:
[quote=引用 11 楼 ri_aje 的回复:] 问题是你的 l 从那来?
嗯,明白了,若是l[]为全局变量的话两者应该没差别吧。[/quote] 没有。不知道 l 是全局的。
ri_aje 2013-07-26
  • 打赏
  • 举报
回复
引用 13 楼 rocktyt2 的回复:
[quote=引用 7 楼 ri_aje 的回复:] 用 lambda 试试

upper_bound(ar.begin(),ar.end(),x,
[&](int const x, // this x is the previous x
    int const index /* this value will come from vector ar*/)
{
 return l[index] < x;
});
标准里似乎有对upper_bound比较函数的两个参数顺序有规定 被比较的value在前,集合内的数据在后 lower_bound则是相反[/quote] 对呀。不过为啥强调这个,我的顺序写反了吗?看了一下好像没有。
qqwqert001 2013-07-26
  • 打赏
  • 举报
回复
引用 13 楼 rocktyt2 的回复:
[quote=引用 7 楼 ri_aje 的回复:] 用 lambda 试试

upper_bound(ar.begin(),ar.end(),x,
[&](int const x, // this x is the previous x
    int const index /* this value will come from vector ar*/)
{
 return l[index] < x;
});
标准里似乎有对upper_bound比较函数的两个参数顺序有规定 被比较的value在前,集合内的数据在后 lower_bound则是相反[/quote] 好像确实lower_bound与upper_bound的比较函数规则不一样,怪不得曾经我使用相同的比较函数,两者返回的结果一个正确一个错误。
qqwqert001 2013-07-26
  • 打赏
  • 举报
回复
引用 11 楼 ri_aje 的回复:
问题是你的 l 从那来?
嗯,明白了,若是l[]为全局变量的话两者应该没差别吧。
rocktyt 2013-07-26
  • 打赏
  • 举报
回复
引用 7 楼 ri_aje 的回复:
用 lambda 试试

upper_bound(ar.begin(),ar.end(),x,
[&](int const x, // this x is the previous x
    int const index /* this value will come from vector ar*/)
{
 return l[index] < x;
});
标准里似乎有对upper_bound比较函数的两个参数顺序有规定 被比较的value在前,集合内的数据在后 lower_bound则是相反
rocktyt 2013-07-26
  • 打赏
  • 举报
回复
引用 10 楼 qqwqert001 的回复:
[quote=引用 9 楼 ri_aje 的回复:] 要求支持新标准,如果你的编译器不支持,还可以使用下面的方法。

struct comp_t
{
 int const* const l;
 comp_t (int const* const local_l) : l(local_l) { }
 bool operator () (int const x, int const index) const
 {
  return l[index] < x;
 }
} const comp(l); // capture the l array.
upper_bound(ar.begin(),ar.end(),x,comp);
实际上 lambda 只不过是编译器帮你做上面手写的这些事儿,所以 lambda 能做到的,不用 lambda 也都能做到,只不过是费不费事的问题。
引用 9 楼 ri_aje 的回复:
要求支持新标准,如果你的编译器不支持,还可以使用下面的方法。

struct comp_t
{
 int const* const l;
 comp_t (int const* const local_l) : l(local_l) { }
 bool operator () (int const x, int const index) const
 {
  return l[index] < x;
 }
} const comp(l); // capture the l array.
upper_bound(ar.begin(),ar.end(),x,comp);
实际上 lambda 只不过是编译器帮你做上面手写的这些事儿,所以 lambda 能做到的,不用 lambda 也都能做到,只不过是费不费事的问题。
我对比较函数的写法了解的比较少,若这样写可以吗? struct cmp { bool operator () (int a,int b) { return l[a]<b; } }; [/quote]这样写,l不在作用域内
ri_aje 2013-07-26
  • 打赏
  • 举报
回复
问题是你的 l 从那来?
qqwqert001 2013-07-26
  • 打赏
  • 举报
回复
引用 9 楼 ri_aje 的回复:
要求支持新标准,如果你的编译器不支持,还可以使用下面的方法。

struct comp_t
{
 int const* const l;
 comp_t (int const* const local_l) : l(local_l) { }
 bool operator () (int const x, int const index) const
 {
  return l[index] < x;
 }
} const comp(l); // capture the l array.
upper_bound(ar.begin(),ar.end(),x,comp);
实际上 lambda 只不过是编译器帮你做上面手写的这些事儿,所以 lambda 能做到的,不用 lambda 也都能做到,只不过是费不费事的问题。
引用 9 楼 ri_aje 的回复:
要求支持新标准,如果你的编译器不支持,还可以使用下面的方法。

struct comp_t
{
 int const* const l;
 comp_t (int const* const local_l) : l(local_l) { }
 bool operator () (int const x, int const index) const
 {
  return l[index] < x;
 }
} const comp(l); // capture the l array.
upper_bound(ar.begin(),ar.end(),x,comp);
实际上 lambda 只不过是编译器帮你做上面手写的这些事儿,所以 lambda 能做到的,不用 lambda 也都能做到,只不过是费不费事的问题。
我对比较函数的写法了解的比较少,若这样写可以吗? struct cmp { bool operator () (int a,int b) { return l[a]<b; } };
ri_aje 2013-07-26
  • 打赏
  • 举报
回复
要求支持新标准,如果你的编译器不支持,还可以使用下面的方法。

struct comp_t
{
 int const* const l;
 comp_t (int const* const local_l) : l(local_l) { }
 bool operator () (int const x, int const index) const
 {
  return l[index] < x;
 }
} const comp(l); // capture the l array.
upper_bound(ar.begin(),ar.end(),x,comp);
实际上 lambda 只不过是编译器帮你做上面手写的这些事儿,所以 lambda 能做到的,不用 lambda 也都能做到,只不过是费不费事的问题。
qqwqert001 2013-07-26
  • 打赏
  • 举报
回复
引用 7 楼 ri_aje 的回复:
用 lambda 试试

upper_bound(ar.begin(),ar.end(),x,
[&](int const x, // this x is the previous x
    int const index /* this value will come from vector ar*/)
{
 return l[index] < x;
});
对,我以前见过这种写法,不过似乎不是所有编译器都支持
ri_aje 2013-07-26
  • 打赏
  • 举报
回复
用 lambda 试试

upper_bound(ar.begin(),ar.end(),x,
[&](int const x, // this x is the previous x
    int const index /* this value will come from vector ar*/)
{
 return l[index] < x;
});
qqwqert001 2013-07-26
  • 打赏
  • 举报
回复
引用 5 楼 turingo 的回复:
那就直接用for循环了,自己写个函数。 [quote=引用 3 楼 qqwqert001 的回复:] [quote=引用 1 楼 turingo 的回复:] 直接用upper_bound(ar.begin(),ar.end(),x)就可以找到>x的第一个数所在的位置。
我要求的不是a>x,而是l[a]>x ~[/quote][/quote] 额,之所以要用upper_bound是想探究一下比较函数的用法。 for循环就不用了,在有序前提下可以写个二分搜索。
图灵狗 2013-07-26
  • 打赏
  • 举报
回复
那就直接用for循环了,自己写个函数。
引用 3 楼 qqwqert001 的回复:
[quote=引用 1 楼 turingo 的回复:] 直接用upper_bound(ar.begin(),ar.end(),x)就可以找到>x的第一个数所在的位置。
我要求的不是a>x,而是l[a]>x ~[/quote]
加载更多回复(4)

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧