std:: lower_bound

该函数返回范围内第一个不小于(大于或等于)指定val的值。如果序列中的值都小于val,则返回last.序列应该已经有序!

eg:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argv,char **argc)
{
vector<int> v1{,,,};
cout<<"v1=";
for(int i:v1)
cout<<i<<" ";
cout<<endl;
auto it=lower_bound(v1.begin(),v1.end(),);
cout<<"lower_bound(v1.begin(),v1.end(),3)="<<*it<<endl;
auto it2=lower_bound(v1.begin(),v1.end(),);
if(it2==v1.end())
cout<<"lower_bound(v1.begin(),v1.end(),5)=v1.end()"<<endl;
else
cout<<"lower_bound(v1.begin(),v1.end(),5)="<<*it2<<endl; }

截图:

std:: upper_bound

该函数返回范围内第一个 大于 指定val的值。如果序列中的值都小于val,则返回last.序列应该已经有序!

eg:

#include <iostream>     // std::cout
#include <algorithm> // std::lower_bound, std::upper_bound, std::sort
#include <vector> // std::vector int main () {
int myints[] = {,,,,,,,};
std::vector<int> v(myints,myints+); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low,up;
low=std::lower_bound (v.begin(), v.end(), ); // ^
up= std::upper_bound (v.begin(), v.end(), ); // ^ std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return ;
}

截图:

另外,在map里的使用方法:

// map::lower_bound/upper_bound
#include <iostream>
#include <map> int main ()
{
std::map<char,int> mymap;
std::map<char,int>::iterator itlow,itup; mymap['a']=;
mymap['b']=;
mymap['c']=;
mymap['d']=;
mymap['e']=; itlow=mymap.lower_bound ('b'); // itlow points to b
itup=mymap.upper_bound ('d'); // itup points to e (not d!) mymap.erase(itlow,itup); // erases [itlow,itup) // print content:
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n'; return ;
}

结果:

a =>
e =>

最新文章

  1. 展开easyui 树节点到某个点
  2. Spring Framework------&gt;version4.3.5.RELAESE-----&gt;Reference Documentation学习心得-----&gt;关于spring framework中的beans
  3. CocoaPods 使用
  4. &lt;极客学院&gt;视频教程学习笔记-iOS中CALayer的使用
  5. 【POJ 1061】青蛙的约会
  6. 查看运行的KVM的虚机
  7. UIView局部点击(转)
  8. 真机无法接收到android.provider.Telephony.SMS_RECEIVED的问题
  9. 安装Stomp扩展时错误提示error: &#39;zend_class_entry&#39; has no member named &#39;default_properties&#39;
  10. Jquery 学习插件第一天
  11. C# 语法技巧_三目运算_switch_case
  12. 《Python学习手册》
  13. ORM-Dapper+DapperExtensions
  14. iOS自动打包并发布脚本
  15. Framework7
  16. luogu4055 游戏 (二分图博弈)
  17. Java第二次上机随笔
  18. centos7 修改网络配置问题
  19. Linux无权限上传文件解决办法
  20. java多线程快速入门(五)

热门文章

  1. freemarket使用自定义标签 注解【项目实际使用】
  2. Android开发的基础知识点
  3. fft蝶形算法的特点
  4. c#同步調用異步(async)方法【記錄用】
  5. cookie跨站脚本漏洞解决方案
  6. wordpress改不了固定连接的解决办法
  7. laravel 二维码生成器包 QrCode 的使用
  8. python基础篇之进阶
  9. 微信小程序,请求php后台返回json数据多出隐藏字符问题
  10. 上一步是硬件描述语言,下一步是FPGA