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 =>

std:: lower_bound std:: upper_bound的更多相关文章

  1. C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique

    std::sort      对vector成员进行排序; std::sort(v.begin(),v.end(),compare);   std::lower_bound 在排序的vector中进行 ...

  2. lower_bound 和 upper_bound

    Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...

  3. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

  4. std::binary_serach, std::upper_bound以及std::lower_bound

    c++二分查找的用法 主要是 std::binary_serach,  std::upper_bound以及std::lower_bound 的用法,示例如下: std::vector<int& ...

  5. STL之std::set、std::map的lower_bound和upper_bound函数使用说明

    由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...

  6. std::lower_bound 功能

    std::lower_bound default (1) template <class ForwardIterator, class T> ForwardIterator lower_b ...

  7. STL源码学习----lower_bound和upper_bound算法

    转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...

  8. vector的插入、lower_bound、upper_bound、equal_range实例

    对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...

  9. 关于lower_bound( )和upper_bound( )的常见用法

    lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...

随机推荐

  1. HAWQ取代传统数仓实践(十三)——事实表技术之周期快照

    一.周期快照简介 周期快照事实表中的每行汇总了发生在某一标准周期,如一天.一周或一月的多个度量.其粒度是周期性的时间段,而不是单个事务.周期快照事实表通常包含许多数据的总计,因为任何与事实表时间范围一 ...

  2. 使用hping3/nping施行DoS攻击

    DDoS攻击是常见的攻击方式,每小时大约发生28次.http://www.digitalattackmap.com提供在世界范围内的DDoS实时攻击分布图: 从DDoS攻击的地图上就可以看出国际形势: ...

  3. Qt TabWidget QTabBar 宽高设置

    /*************************************************************************** * Qt TabWidget QTabBar ...

  4. C++11新特性之线程操作

    C++11之前没有对并发编程提供语言级别的支持,这使得我们在编写可移植的并发程序时,存在诸多的不便.现在C++11增加了线程以及线程相关的类,很方便地支持了并发编程,使得编写的多线程程序的可移植性得到 ...

  5. 在Spring中通过构造自动装配--constructor

    在Spring中,可以使用“通过构造自动装配”,实际上是按构造函数的参数类型自动装配. 这意味着,如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么将自动装配. packag ...

  6. ajax同步异步

    test.html <a href="javascript:void(0)" onmouseover="testAsync()"> asy.js f ...

  7. Source Insight中文字体设置

    Source Insight是一个面向项目开发的程序编辑器和代码阅读工具,它拥有内置的对C/C++, C#和Java等程序的分析,分析你的源代 码并在你工作的同时动态维护它自己的符号数据库,并自动为你 ...

  8. c++ 浅谈 new 的使用

    实例用法: 创建对象: class U_Ptr smart; U_Ptr* ptr = new U_Ptr(); class U_Ptr smart(new U_Ptr(p)); int *p = n ...

  9. fragment用法

    简单用法: 1.新建布局.新建fragment类 2.在activity_main.xml中添加fragment <LinearLayout...... <fragment android ...

  10. java代码----equals和==区别

    总结: ==的意义在于比较的是整型 package com.aa; // public class Bd { public static void main(String[] args) { Inte ...