1.unique(去重)函数2.二分函数lower_bound和upper_bound3.取整函数(ceil、floor、round)
1.unique(去重)函数现在总结一下unique,unique的作用是“去掉”容器中相邻元素的重复元素(不一定要求数组有序),它会把重复的元素添加到容器末尾(所以数组大小并没有改变),而返回值是去重之后的尾地址,下面举个例子。
由于返回的是容器末尾,所以如果想得到去重后的size,需要减去初始地址;
size = unique(b + 1,b + n + 1)-(b + 1); size = unique(a,a + n) - a;
#include#include using namespace std; int main() { const int N=11; int a[N]={1,2,0,3,3,0,7,7,7,0,8}; int n = unique(a,a+N)-a; for(int i=0;i 2.二分函数lower_bound和upper_bound 使用这两个函数的前提条件就是,序列为非递减序列
#includeusing namespace std; const int maxn=100000+10; const int INF=2*int(1e9)+10; #define LL long long int cmd(int a,int b){ return a>b; } int main(){ int num[6]={1,2,4,7,15,34}; sort(num,num+6); //按从小到大排序 int pos1=lower_bound(num,num+6,7)-num; //返回数组中第一个大于或等于被查数的下标 int pos2=upper_bound(num,num+6,7)-num; //返回数组中第一个大于被查数的下标 cout< ())-num; //返回数组中第一个小于或等于被查数的值 int pos4=upper_bound(num,num+6,7,greater ())-num; //返回数组中第一个小于被查数的值 cout< 3.取整函数(ceil、floor、round) 大佬总结
ceil:在英文中,是天花板的意思,有向上的意思,所以,此函数是向上取整
floor:在英文中,是地面,地板的意思,有下面的意思,所以,此函数是向下取整
round:在英文中是有大约,环绕,在某某四周,附近的意思,所以,可以取其大约的意思,在函数中是四舍五入#include#include using namespace std; int main() { double x1 = 7.4; double x2 = 7.5; cout<



