栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

比较有用的函数(私用,持续更新)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

比较有用的函数(私用,持续更新)

文章目录

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 

使用这两个函数的前提条件就是,序列为非递减序列

#include
using 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<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/702233.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号