首页 > 其他分享 >常用库函数

常用库函数

时间:2023-01-09 12:44:30浏览次数:37  
标签:常用 main cout int namespace bound include 库函数

1.reverse和unique

#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> v({1,2,3,4,5,5});
    reverse(v.begin(),v.end());//O(n)
    for(auto x:v) cout<<x<<" ";
    cout<<endl;
    int a[]={1,2,2,3,3};
    //unique会对相同元素在一起的去重
    int m=unique(a,a+5)-a;cout<<m<<endl;
    //m为地址间的差值
    for(int i=0;i<m;++i) cout<<a[i]<<" ";
    cout<<endl;
    m=unique(v.begin(),v.end())-v.begin();cout<<m<<endl;
    v.erase(unique(v.begin(),v.end()),v.end());
    for(auto x:v) cout<<x<<" ";
}

2.random_shuffle

#include<algorithm>
#include<iostream>
#include<vector>
#include<ctime>
using namespace std;
int main()
{
    srand(time(0));
    int a[]={1,2,3,4,5};
    random_shuffle(a,a+5);
    for(auto x:a) cout<<x<<" ";
}

3.sort

#include<algorithm>
#include<iostream>
#include<vector>
#include<ctime>
struct st{
   int a,b;
   //重载运算可以看作当前结构体是否排在x前面
   bool operator < (const st x) const{
      return a>x.a;
   }
}s[2];
bool cmp(int a,int b)//cmp可以看作a是否应该排在b的前面
{
    return (a%2)&&!(b%2);
}
using namespace std;
int main()
{
    s[0]={1,2};
    s[1]={2,1};
    sort(s,s+2);
    cout<<s[0].a<<" "<<s[0].b<<endl;
    int p[]={1,2,3,4,5};
    sort(p,p+5,cmp);//cmp实现了将奇数排在偶数前
    for(auto x:p) cout<<x<<" ";
}

4.upper_bound和lower_bound

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{

    int a[]={1,2,4,5};
    int *p=lower_bound(a,a+4,2);
    int *q=upper_bound(a,a+4,2);
    cout<<*p<<" "<<*q<<endl;
    int m=p-a,n=q-a;
    cout<<m<<" "<<n<<endl;//得到下标
    cout<<q-p<<endl;//得到2出现的次数
}

标签:常用,main,cout,int,namespace,bound,include,库函数
From: https://www.cnblogs.com/ruoye123456/p/17036704.html

相关文章

  • 求解连通图问题的常用方法
    判断图是否连通以及连通子图个数的问题一般常见的解法是并查集或者图的遍历(dfs/bfs)连通图经典并查集写法#include<bits/stdc++.h>usingnamespacestd;constintN......
  • 【linux】linux Centos8系统,防火墙配置常用命令,systemctl 和firewall
    本文环境:Linux系统CentOS8.264bitCentOS7版本及以上版本较centos6有较大改动,例如:采用systemctl命令来开启service,它是服务管理中主要的工具,融合了之前service和chkconf......
  • Git配置免密登录及常用操作的详细教程(基于Gitee平台)
    (文章目录)前言我这里使用的是vuecli创建的项目进行代码管理,使用的平台是Gitee。平台的话其实最推荐使用的平台还是GitHub(但是因为国内连接不稳定的原因放弃使用),因为Git......
  • MySQL 常用脚本
    1.导出整个数据库 1mysqldump -u 用户名 -p –default-character-set=latin1 数据库名 > 导出的文件名(数据库默认编码是latin1)  23mysqldump -u wcnc -p s......
  • pom.xml经常用到去写的坐标
    tomcat7插件1<plugin>2<groupId>org.apache.tomcat.maven</groupId>3<artifactId>tomcat7-maven-plugin</artifactId>4</plugin>jdk17坐标,因......
  • docker-compose常用命令
    build:本地创建镜像command:覆盖缺省命令depends_on:链接容器ports:暴露端口volumes:卷image:pull镜像up:启动stop:停止rm:删除logs:查看日志ps:列出服务相关容器 ......
  • 四个常用的域对象
    四个常用的域对象:看了下web课上的PPT,我承认之前太大声了╥﹏╥四大域对象常用的四大域,即pageContext,request,session,application功能:都是存取数据,但对数据的存取范围不同......
  • C#枚举的常用用法
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApp3{enummyenu......
  • Windchill_二次开发新手入门常用的API
    Windchill_二次开发新手入门常用的API 1.根据零件名称/编码得到该零件   wt.clients.prodmgmt.WTPartHelper.findPartByName(name);   wt.clients.prodm......
  • HTML_2_常用head标签
    head标签是html组成的一个部分,主要用于配置页面信息。  标题标签:<title>网页标题!</title>编码格式标签:<!--编码配置:html5--><metacharset="UTF-8"><!--编......