STL algorithm算法lower_bound和upper_bound(31)

STL algorithm算法lower_bound和upper_bound(31)

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

lower_bound原型:

function template
<algorithm>

std::lower_bound

default (1)
template <class ForwardIterator, class T>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
                               const T& val);
custom (2)
template <class ForwardIterator, class T, class Compare>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
                               const T& val, Compare comp);

该函数返回范围内第一个
不小于(大于或等于)指定val的值。

假设序列中的值都小于val,则返回last.

序列应该已经有序!

使用operator<来比較两个元素的大小。

该函数优化了比較非连续存储序列的比較次数。

其行为类似于:

template <class ForwardIterator, class T>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
  ForwardIterator it;
  iterator_traits<ForwardIterator>::difference_type count, step;
  count = distance(first,last);
  while (count>0)
  {
    it = first; step=count/2; advance (it,step);
    if (*it<val) {                 // or: if (comp(*it,val)), for version (2)
      first=++it;
      count-=step+1;
    }
    else count=step;
  }
  return first;
}

一个简单的样例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argv,char **argc)
{
	vector<int> v1{1,2,3,4};
	cout<<"v1=";
	for(int i:v1)
		cout<<i<<" ";
	cout<<endl;
	auto it=lower_bound(v1.begin(),v1.end(),3);
	cout<<"lower_bound(v1.begin(),v1.end(),3)="<<*it<<endl;
	auto it2=lower_bound(v1.begin(),v1.end(),5);
	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;

}

执行截图:

STL algorithm算法lower_bound和upper_bound(31)


upper_bound原型:

function template
<algorithm>

std::upper_bound

default (1)
template <class ForwardIterator, class T>
  ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
                               const T& val);
custom (2)
template <class ForwardIterator, class T, class Compare>
  ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
                               const T& val, Compare comp);

该函数返回范围内第一个大于指定val的值。

假设序列中的值都小于val,则返回last.

序列应该已经有序!

使用operator<来比較两个元素的大小。

该函数优化了比較非连续存储序列的比較次数。

其行为类似于:

template <class ForwardIterator, class T>
  ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
  ForwardIterator it;
  iterator_traits<ForwardIterator>::difference_type count, step;
  count = std::distance(first,last);
  while (count>0)
  {
    it = first; step=count/2; std::advance (it,step);
    if (!(val<*it))                 // or: if (!comp(val,*it)), for version (2)
      { first=++it; count-=step+1;  }
    else count=step;
  }
  return first;
}

一个简单的样例:

// lower_bound/upper_bound example
#include <iostream>     // std::cout
#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort
#include <vector>       // std::vector

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  std::vector<int> v(myints,myints+8);           // 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(), 20); //          ^
  up= std::upper_bound (v.begin(), v.end(), 20); //                   ^

  std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
  std::cout << "upper_bound at position " << (up - v.begin()) << '\n';

  return 0;
}

执行结果:

STL algorithm算法lower_bound和upper_bound(31)

lower_bound和upper_bound返回的值刚好是equal_range相应的两个值!


——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,能够在以下留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我改动,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-17

于GDUT

——————————————————————————————————————————————————————————————————





版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118253.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 什么是虚拟ip地址_虚拟人IP是什么意思

    什么是虚拟ip地址_虚拟人IP是什么意思AIX中虚拟IP地址的概念与IBMOS/390中的很相似。将虚拟IP地址赋给AIX系统后,可以使IP地址不再依赖指定的网络接口。发送方只需将包送到接收方服务器的虚拟IP地址上即可(所有接收到的包还是通过真正的物理网络接口到达该服务器的)。在虚拟IP地址使用以前,如果一个网络接口失效,所有与之相关的连接(connection)就都会失去。使用虚拟IP地址,需要有AIX系统对虚拟接口和网

    2022年10月20日
  • bs模型与cs模型共同点_bs模型的基本假设

    bs模型与cs模型共同点_bs模型的基本假设bs模式客户端通过浏览器,浏览web服务器上的网页,这样的模型叫bs模型,b指客户端browser,s指服务端server。在客户端和浏览器端之间走的报文是http协议(即超文本传输协议) cs模型客户端(client)发报文,服务器(server)收报文,服务器收到报文之后处理。这与bs模式没有很大区别,只不过是c与s间可以自定义数据传送报文。cs模式一般走的协议是tcp协议

    2022年9月14日
  • EJB初步学习

    EJB初步学习 今天简单学习了传说中的EJB,首先总的感觉,就是他的最重要的一个特点吧,就是能够使远程用户访问到本地或是服务器上的资源服务器。打个比方吧,传统的,还记得我们的第一个JAVA项目吧,那是个简单的对数据库增删改查的操作,用简单的界面来显示数据。那么当我们把这个项目打包发布之后,事必要把你自己的数据库也贡献出去,你做的软件在进行增删改查时也就只能对你机子上的一个数据库,别人如果想要对你这个数据库进

    2022年9月30日
  • 磁盘,硬盘,软盘,光盘的区别[通俗易懂]

    磁盘,硬盘,软盘,光盘的区别[通俗易懂]计算机存储器分为两大类:内存存储器和外部存储器(简称内存或内存条和外存)。内存容量小,存取速度快,只能临时保存信息(经cup处理后的数据),断电后信息就会消失。外存容量大,存取速度比内存慢,能永久

    2022年8月3日
  • C++ map 根据value找key、 根据key找value

    C++ map 根据value找key、 根据key找value根据value找key    有可能找到多个结果根据key找value    、、、、、、运行效果:代码很简单,如下:#include&lt;iostream&gt;#include&lt;map&gt;#include&lt;string&gt;usingnamespacestd;intmain(intargc,char*…

    2022年7月23日
  • 理解的英文_Spring ioc

    理解的英文_Spring ioc前言:这是在慕课网上学习SpringBoot2.0深度实践之核心技术篇时所做的笔记,主要供本人复习之用。目录第一章概要1.1基本使用1.1.1直接运行1.1.2自定义1.2用到的基础技术1.3用到的扩展衍生技术第二章SpringApplication的准备阶段2.1配置SpringBean的来源2.2推断Web应用类型2.3推断引…

    2022年9月10日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号