lower_bound() 在数组中搜索时

搜不到

返回 .end(),

若需要返回0,用upper_bound()-lower_bound()

若要返回下一个下标  则需要在set / multiset 中使用lower_bound()

下面是测试代码及样例

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<list>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> p;
typedef long double ld;
#define mem(x) memset(x, 0, sizeof(x))
#define me(x) memset(x, -1, sizeof(x))
#define fo(i,n) for(i=0; i<n; i++)
#define sc(x) scanf("%lf", &x)
#define pr(x) printf("%lld\n", x)
#define pri(x) printf("%lld ", x)
#define lowbit(x) x&-x
const ll MOD = 1e18 +;
const ll N = 6e6 +;
set<ll> s;
int main()
{
ll i, j, k, l=;
ll n, m, t;
//cin>>n;
n=;
for(i=; i<n; i++)
s.insert(i);
set<ll>::iterator it;
cout<<"输出set"<<endl;
for(it=s.begin(); it!=s.end(); it++) k=*it,cout<<k<<" ";cout<<endl;
while(cin>>k)
{
it=s.lower_bound(k);
t=*it;
//if(t==k)
//cout<<"找到"
cout<<t<<endl;
if(it!=s.end())
{
if(t==k)
cout<<"找到"<<t<<endl<<endl;
else cout<<"未找到 返回下一个下标 输出*(it+1) "<<t<<endl<<endl;
s.erase(it);
}
else
{
cout<<"返回 s.end()下标 "<<t<<endl<<endl;
}
for(it=s.begin(); it!=s.end(); it++) k=*it,cout<<k<<" ";cout<<endl<<endl;
}
return ;
}

算法 set / multiset -- lower_bound()的二分搜索的更多相关文章

  1. STL 源代码剖析 算法 stl_algo.h -- lower_bound

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie lower_bound(应用于有序区间) ------------------------- ...

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

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

  3. 代码题(1)—lower_bound和upper_bound算法

    1.lower_bound:查找序列中的第一个出现的值大于等于val的位置 这个序列中可能会有很多重复的元素,也可能所有的元素都相同,为了充分考虑这种边界条件,STL中的lower_bound算法总体 ...

  4. 不光是查找值! "二分搜索"

    2018-11-14 18:14:15 二分搜索法,是通过不断缩小解的可能存在范围,从而求得问题最优解的方法.在程序设计竞赛中,经常会看到二分搜索法和其他算法相结合的题目.接下来,给大家介绍几种经典的 ...

  5. Effective STL

    第9条:慎重选择删除元素的方法 删除特定值元素,vector.string.deque用erase-remove:c.erase(remove(c.begin(),c.end(),1963),c.en ...

  6. ###《Effective STL》--Chapter5

    点击查看Evernote原文. #@author: gr #@date: 2014-09-17 #@email: forgerui@gmail.com Chapter5 算法 Topic 30: 确保 ...

  7. POJ 2533 Longest Ordered Subsequence - from lanshui_Yang

    题目大意:求一个数列的最长上升子序列(严格上升). 解题思路: 方法一:O(n^2) dp[i]:表示处理到第i个位置,序列的最长上升子序列末尾为i的长度: a[]数组存储原序列 dp[i] = ma ...

  8. 最近公共祖先(LCA)的三种求解方法

    转载来自:https://blog.andrewei.info/2015/10/08/e6-9c-80-e8-bf-91-e5-85-ac-e5-85-b1-e7-a5-96-e5-85-88lca- ...

  9. HDU_5532_Almost Sorted Array

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

随机推荐

  1. cacheline基本理论

    一.cacheline 1.cache:解决cpu频率与内存访问之间速度差距越来越大的问题 2.cacheline:cpu cache的最小单位,主流为64B 3.指导:访问数组数据在同一个cache ...

  2. Unable to find a constructor to use for type System.Security.Claims.Claim. A class should either have a default constructor

    Newtonsoft.Json DeserializeObject 反序列化  IdentityServer4.Models Cliecnt 错误: Newtonsoft.Json.JsonSeria ...

  3. 从零开始一起学习SLAM | 掌握g2o顶点编程套路

    点"计算机视觉life"关注,置顶更快接收消息! ## 小白:师兄,上一次将的g2o框架<从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码>真的很清晰 ...

  4. Leetcode: Encode and Decode TinyURL

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  5. python数据类型之字典类型

    # 字典常用方法clear(), get(), pop(), update(),copy(),items(), popitem(),values(), fromkeys(), keys(),setde ...

  6. qt 画多边形(实现鼠标拖动节点)

    ---恢复内容开始--- 2018-01-06 这个小例子实现了移动鼠标,鼠标的坐标信息跟随鼠标移动,多边形的实现,鼠标点击可以拖动多边形点的位置,(其中有个问题?我在QMainWindow下,用mo ...

  7. C# Window Service安装、卸载、恢复选项操作

    using System;using System.Diagnostics;using System.Linq;using System.ServiceProcess; namespace ScmWr ...

  8. pydemo_testMaopuSpider

    import json from multiprocessing import Pool import requests from requests.exceptions import Request ...

  9. App 基本图片配置(I)

    UITabBarController 的底部Item图片设置 分为三种: imageName@1x.png 图片像素(30 * 30) imageName@2x.png 图片像素(60 * 60) i ...

  10. 2017.12.7 URAT 串口通信

    波特率就是发送二进制数据位的速率, 习惯上用 baud 表示, 即我们发送一位二进制数据的持续时间=1/baud. 在通信之前, 单片机 1 和单片机 2 首先都要明确的约定好它们之间的通信波特率, ...