std:: lower_bound std:: upper_bound
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的更多相关文章
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
std::sort 对vector成员进行排序; std::sort(v.begin(),v.end(),compare); std::lower_bound 在排序的vector中进行 ...
- lower_bound 和 upper_bound
Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
- std::binary_serach, std::upper_bound以及std::lower_bound
c++二分查找的用法 主要是 std::binary_serach, std::upper_bound以及std::lower_bound 的用法,示例如下: std::vector<int& ...
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- std::lower_bound 功能
std::lower_bound default (1) template <class ForwardIterator, class T> ForwardIterator lower_b ...
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- 关于lower_bound( )和upper_bound( )的常见用法
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数 ...
随机推荐
- Windows GVLK密钥对照表(KMS激活专用
以下key来源于微软官网:https://technet.microsoft.com/en-us/library/jj612867.aspx Windows Server 2016 操作系统 KMS激 ...
- FZU Problem 2129 子序列个数
看了 dp 方程之后应该是妙懂 每次 加入一个数,×2 然后剪掉重复的: 重复的个数 维前面那个数,,,,, #include<iostream> #include<stdio.h ...
- python库之threading
This module constructs higher-level threading interfaces on top of the lower level python库之_threadmo ...
- bzoj 1725 Corn Fields
Written with StackEdit. Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行\((1<=M<=12; 1<=N< ...
- 【转载】Python正则表达式指南
本文转自:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html#!comments 1. 正则表达式基础 1.1. 简单介绍 正则表达 ...
- iOS6和iOS7代码的适配(5)——popOver
popOver这个空间本身是iPad only的,所以iPhone上见不到,我记得微信上有个这样的弹出框,有扫一扫等几个菜单项,估计这是腾讯自己实现的,用于菜单的扩展. popOver从iOS6到iO ...
- 剑指offer-第六章面试中的各项能力(圆圈中剩下的最后数字)
import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util ...
- 剑指offer-第五章优化时间和空间效率(连续子数组的最大和)
题目:输入一个数组,数组中有正也有负,数组中连续的一个或者连续的多个数字组成一个子数组.求所有的子数组和的最大值.要求时间复杂度为O(n) 思路:我们的最直观的想法就是求出这个数组中的所有的子数组,然 ...
- DbEntry 默认 主键ID为long
DbEntry 默认 主键ID为long,如果自己表中的主键ID为int,可以通过以下方式修改: public class Company :DbObjectModel<Company,int& ...
- MAC OS X常用命令总结2
1. dir:显示某个目录下的子目录与文件. 格式:dir [x:] [Path] [filename][ parameter] 参数解释: /a 显示所有文件夹与文件. /p 分页 ...