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):从数 ...
随机推荐
- OpenCV中阈值(threshold)函数: threshold 。
OpenCV中提供了阈值(threshold)函数: threshold . 这个函数有5种阈值化类型,在接下来的章节中将会具体介绍. 为了解释阈值分割的过程,我们来看一个简单有关像素灰度的图片,该图 ...
- animal farm 第一章阅读笔记
chapter 1 Old Major's dream. paragraph 1 //Mr Jones is the mastor of the Manor Farm.That night ...
- computed属性与methods、watched
一.计算属性 new Vue({ data: { message: 'hello vue.js !' }, computed: { reverseMessage: function () { retu ...
- C++笔记之CopyFile和MoveFile的使用
1.函数定义 CopyFile(A, B, FALSE);表示将文件A拷贝到B,如果B已经存在则覆盖(第三参数为TRUE时表示不覆盖) MoveFile(A, B);表示将文件A移动到B 2.函数原型 ...
- 笔记:使用 Protel 99 SE 改一块车充 PCB
笔记:使用 Protel 99 SE 改一块车充 PCB Protel 99 SE N 多年前用过,之前就再没有碰过了. 今天由于特殊原因又使用了一下. 还好有些有印象,现记录一下以免忘记. 元件转向 ...
- Restore Nexus 5 to Stock and Flash Factory Images
1.This is the website to download Factory Images for Nexus Devices https://developers.google.com/and ...
- mysql权限验证流程
mysql用户管理,逐级下查 mysql库的user表连接信息,全局权限db表记录用户对库的权限,对某个数据库的所有表的权限tables_priv 设置用户对表的权限columns_priv设置用户对 ...
- POJ_3740 Easy Finding ——精确覆盖问题,DLX模版
Easy Finding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18790 Accepted: 5184 Des ...
- Oracle10g客户端连接远程数据库配置图解
yuanwen:http://blog.csdn.net/DKZhu/article/details/6027933 一. 安装oracle客户端 1. 运行setup.exe,出现 2. ...
- 赋予oracle执行存储过程权限和创建表权限
grant create any table to username; grant create any procedure to username; grant execute any proced ...