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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 照片切割器_切割图片的软件

    照片切割器_切割图片的软件两款图片切割工具ShoeBox:http://renderhjs.net/shoebox/BigShear:https://www.fancynode.com.cn/bigshear下面试下Sh

    2022年8月3日
    4
  • 最小生成树的两种方法(Kruskal算法和Prim算法)[通俗易懂]

    关于图的几个概念定义:连通图:在无向图中,若任意两个顶点vivi与vjvj都有路径相通,则称该无向图为连通图。 强连通图:在有向图中,若任意两个顶点vivi与vjvj都有路径相通,则称该有向图为强连通图。 连通网:在连通图中,若图的边具有一定的意义,每一条边都对应着一个数,称为权;权代表着连接连个顶点的代价,称这种连通图叫做连通网。 生成树:一个连通图的生成树是指一个连通子图,它含有图中…

    2022年4月6日
    47
  • 模电–运算放大器工作原理

    模电–运算放大器工作原理模电领悟1(关于正负反馈是通过瞬时极性法判断净输入量的增减,与净输入量的正负号无关,与他的量有关,增了就是正反馈;所谓的同相反相输入端是指相位,反相与同相相位差为180°,所以如果同相和反相加的是同一个电压,…

    2022年6月3日
    29
  • x86平台inline hook原理和实现

    x86平台inline hook原理和实现概念inlinehook是一种通过修改机器码的方式来实现hook的技术。原理对于正常执行的程序,它的函数调用流程大概是这样的:0x1000地址的call指令执行后跳转到0x3000地址处执行

    2022年7月2日
    24
  • Using ZipLib to create a Zip File in C#

    Using ZipLib to create a Zip File in C#System.IO.Compression是.Net2.0里与压缩有关的命名空间,但是使用起来并不是很方便。使用第3方库ziplib可以很方便地进行压缩类的操作。从[1]下载动态库,然后在工程里AddReference,把ICSharpCode.SharpZipLib.dll加进去。在代码来创建一个zip包的例子如下(摘自ziplibsamplecode)…

    2022年7月26日
    6
  • 三元运算符用法_三元运算符判断三个值

    三元运算符用法_三元运算符判断三个值一、三元运算符条件运算符(?:)也称为三元条件运算符,用于计算布尔表达式,并根据布尔表达式的计算结果为true还是false来返回(使用三元运算符可以简化If…else)。二、三元运算符语法判断条件?:结果1,结果2三、示例比如判断结果0是男生,否则是女生:①使用if…else编写stringresult=null;if(sexValue==0){result=”男”;}else{result=”女”;…

    2022年9月1日
    2

发表回复

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

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