unique & lower_bound C++
原来C++也有unique和lower_bound,只需头文件iostream
- unique
unique可以对数组进行相邻元素的“去重”,实现效果是把所有不重复的元素按顺序放在数组前面,剩余元素留在末尾。函数返回的是指针,表示最后一个唯一元素的位置的下一个地址值
假设a为大小为n的数组,且已经排好序,那么m=unique(a,a+n)-a,则m就是a中unique元素的个数,且a[:m]是已经去重后的数组,看一下例子就明白了:
- int a[] = {,,,,,,};
- cout<<unique(a,a+)-a<<endl;
- //
- for(int i=;i<;i++)
- cout<<a[i]<<" ";
- //1 2 3 4 6 4 6
- lower_bound
lower_bound(a,a+n,target)返回指向a[:n]中第一个≥target的元素的指针,用法如下:
- int a[] = {,,,,,,};
- cout<<lower_bound(a,a+,)<<endl;
- //0xbffe70
- cout<<lower_bound(a,a+,)-a<<endl;
- //
- cout<<lower_bound(a,a+,)-a<<endl;
- //
unique & lower_bound C++的更多相关文章
- [模板总结] Java的一些模板
快速排序(数组a从小到大,参数1是待排序的数组,参数2是起始下标,参数3是终止下标): static void sort(int [] a, int l,int r){ int m = l+r> ...
- POJ2528 Uva10587 Mayor's posters
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- bzoj1935 [Shoi2007]园丁的烦恼
bzoj1935 [Shoi2007]园丁的烦恼 有N个点坐标为(xi,yi),M次询问,询问(a,b)-(c,d)的矩形内有多少点. 0≤n≤500000,1≤m≤500000,0≤xi,yi≤10 ...
- ACM中值得注意/利用的C++语法特性
C++ 的易踩坑点 随时补充 STL不能边循环边erase() //自己写的求交集RE了 for (set <int> ::iterator it = s.begin(); it != s ...
- 「STL中的常用函数 容器」
占个坑,下午在更 二分操作:lower_bound和upper_bound 存图/数列操作:vector容器 全排列:next_permutation和prev_permutation 字符串转数列: ...
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
std::sort 对vector成员进行排序; std::sort(v.begin(),v.end(),compare); std::lower_bound 在排序的vector中进行 ...
- Maximum Value(unique函数,lower_bound()函数,upper_bound()函数的使用)
传送门 在看大佬的代码时候遇到了unique函数以及二分查找的lower_bound和upper_bound函数,所以写这篇文章来记录以备复习. unique函数 在STL中unique函数是一个去重 ...
- STL中的unique()和lower_bound ,upper_bound
unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...
- unique
作用: 元素去重,即“删除”序列中所有相邻的重复元素(只保留一个) (此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素占领了) (其实就是把多余的元素放到了最后面) 由于它“删除”的是相 ...
随机推荐
- 慢SQL汇总
select count(*) as aggregate from `yqz_feed_praise` where `uid` = '580242' and `praise_uid` <> ...
- Lavarel artisan 命令
[alex@iZ25c5aeyiiZ yiqizou3.0]# php artisan list Laravel Framework version Usage: command [options] ...
- Android Annotations(1)
特性: Android Annotations是一个开源的框架,用于加速 Android应用的开发,可以让你把重点放在功能的实现上,简化了代码,提升了可维护性. 特性: 依赖注入: inject ...
- socket__服务端于客户端
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017/8/23 15:33 # @Author : Mr_zhang # @Site ...
- substance在java swing中使用注意事项
package org.dgw.uidemo; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.UI ...
- 结对编程之<GoldPointGame>
我们结对项目的名称是GoldPointGame,这是一个非常经典的老游戏,其规则大致是:N个同学每个人依次报一个(1-100)的有理数,交给裁判算出其平均值,然后乘以黄金点数0.618,最后得到G值. ...
- 原生javascript跨浏览器常用事件处理
var eventUntil = { getEvent: function (event) {//获取事件 return event ? eve ...
- LeetCode 346. Moving Average from Data Stream (数据流动中的移动平均值)$
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- JDBC的基本用法
一.编程步骤 1.加载驱动 Class forName("com.mysql.jdbc.Driver"):mysql驱动 Class forName("oralce.jd ...
- ASP.NET中登录时记住用户名和密码(附源码下载)--ASP.NET
必需了解的:实例需要做的是Cookie对象的创建和对Cookie对象数据的读取,通过Response对象的Cookies属性创建Cookie,通过Request对象的Cookies可以读取Cookie ...