常用函数 c++

常用函数

 

 

1. 常用C++函数

<cstdio>

intscanf("%[*][width][modifiers]type",type_name);

scanf("%2X",&num);

//读取两位16进制数

scanf("%*d%d",&num);

//*读跳过该读取值赋值

scanf("%[a-zA-Z]",str);

//遇到非a-zA-Z之间的字符时结束输入

scanf("%[^a-z]",str);

//遇到a-z之间的字符时结束输入

 

intsscanf(str,"%[*][width][modifiers]type",type_name);

  //从字符串提取数据并赋值给相应变量

intsprintf(str,"%[*][width][modifiers]type",type_name);

  //从其他类型变量转化为字符串

 

 

//文件操作

freopen(file_name,"r",stdin);//更改标准输入为文件输入

freopen(file_name,"w",stdout); //更改标准输出为文件输出

 

FILE *fin,*fout;

fin=fopen("file.in","r");

fout=fopen("file.out","w");

fscanf(fin,"%[*][width][modifiers]type",type_name);

fprintf(fout,"%[*][width][modifiers]type",type_name);

fclose(fin);

fclose(fout);

 

 

<cstring>

memset(str,0x00,sizeof(str));

  //整体赋值,可以将int型变量整体赋值为0-1,用于数组初始化

memcpy(str1,str2,strlen(str2));

  //整体复制,将str2长度的字符串复制到str1中,可用于任何类型

 

int len=strlen(str); //得到字符串长度

 

strcmp(str1,str2);

  //按字典序比较字符串str1str2

  //如果str1>str2返回1;如果str1<str2返回-1;如果相等返回0

 

strcpy(str1,str2); //将字符串str2复制到str1的位置

strcat(str1,str2); //将字符串str2复制到str1的结尾

 

 

<cctype>

isdigit(ch); //是否是数字[0-9]

isalpha(ch); //是否是英文字符[a-zA-Z]

islower(ch); //是否是小写字符[a-z]

isupper(ch); //是否是大写字符[A-Z]

 

tolower(ch); //转化为小写字符

toupper(ch); //转化为大写字符

 

 

<cmath>

pow(double num,double exp); //numexp次方

sqrt(double num); //num开平方

log10(double num); //num10为底的对数

 

sin(rad); cos(rad); tan(rad); //rad弧度的三角函数值

asin(val); acos(val); atan(val); //求反三角函数值

atan2(doublexdoubley); //atan(x/y),返回弧度值

 

 

<cstdlib>,<ctime>

rand();//生成0-32767间的随机数

srand(unsigned Seed); //随机数种子

time(NULL); //1970年1月1日到现在的毫秒数

 

 

<iostream>,<iomanip>

//cout输出小数位数控制

cout.setf(ios::fixed);

cout<<setprecision(2)<<(double)0.1<<endl;//输出0.10

cout.unsetf(ios::fixed);

cout<<setprecision(2)<<(double)0.1<<endl;//输出0.1

 

 

<sstream>

stringstream ss(str);

while(ss>>n){}//不确定个数的同种类型变量读入

 

 

2. 常用STL

 

Container<Type> Container_name;

size() //返回元素个数,unsigned

empty() //判断是否为空,空返回true

 

iterator:

begin() //头迭代器

end() //尾迭代器

Container<Type>::iterator Itor_name;

 

<vector>

clear() //清空容器

swap(vector<T> vec) //交换容器

push_back(T item) //向后添加一个元素

pop_back() //删除最后一个元素

insert(iterator ItorT item) //Itor处插入一个元素

erase(iterator Itor) //删除Itor处元素

 

<list>

//无法使用[]访问元素,可以使用迭代器

clear() //清空容器

push_front();push_back()//链表添加元素

pop_front();pop_back()//链表删除元素

front();back()//访问链表元素

 

<stack>

//FILO,先进后出

push(); pop(); top(); //栈操作

 

<queue>

//FIFO,先进先出,无clear()函数

push(); pop() //队列添加删除操作

 

queue:

front(); back(); //队列取值操作

 

priority_queue:

top() //取队列首项

priority_queue<Type,vector<Type>,CMP>QUE;

priority_queue<Type,vector<Type>,greater<Type>>QUE;

structCMP{

    booloperator() (constType&a,constType& b){

        //sort函数cmp函数写法相同,排列顺序相反

//如果是结构体建议使用小于号重载

    }

};

 

<string>

length() //返回字符串长度,unsigned

operator > < == //字符串字典序比较

operator + //连接两个字符串

c_str() //转换为c字符串

 

<map>

map<key_type,val_type> Map;

  //如果key_type为结构体,需要为结构体添加’<’号重载

Map[key]=val;

clear() //清空容器

 

<bitset>

bitset<Size> Type_Name;

set() //全部初始化为1

reset() //全部初始化为0

flip() //全部置反 (0->1 1->0)

count() //返回set1的个数

any() //set中是否全为真值

none() //set中是否全为假值

 

 

<algorithm>

sort:

sort(Itor begin,Itor end);

sort(Itor begin,Itor end,bool CMP());

bool CMP(const Type& a,constType& b){

    returna>b; //从大到小排序

    returna<b; //从小到大排序

}

 

fill(Itor,Itor,Val);//for循环赋值

reverse(Itor,Itor); //倒置序列

swap(Val,Val); //交换连个变量的值

min(Val,Val); max(Val,Val); //取两者最值

 

 

3. 常用语句

 

//二分加速求幂

int qpow(int val,int exp,int mod){

    int ans;

    for(ans=1;exp;exp>>=1){

       if(exp&1)ans=(ans*val)%mod;

       val=(val*val)%mod;

    }

    return ans;

}

 

//快速求得一个数二进制状态的1的个数

int cntbit(int num){return num?cntbit(num&(num-1))+1:0;}

 

//快速得到一个数的二进制状态的所有子集,用于hash子集

for(int i=cnt;i;i=cnt&(i-1)) vis[i]=true;

 

//随机排序

for(int i=0;i<n;i++) swap(num[i],num[rand()%n]);

 

4. 常用哈希

 

//字符串hash

unsigned BKDRHash(char str[]){

    unsigned _s=131,_h=0;

    for(int i=0;str[i];i++){

       _h=_h*_s+str[i];

    }

    return(_h&0x7FFFFFFF);

}

 

//散列表hash

LL vis[1000007];

inlineint _hash(LL _h,int x){

    int p=_h%1000007,cnt=0;

    while(true){

       if(vis[x][p]==-1) return p;

       if(vis[x][p]!=-1&&vis[x][p]==_h)return p;

       p++; if(p>=1000007)p=0;

    }

    return -1;

}

 

//逆序数hash

constint fac[]={1,1,2,6,24,120,720,5040,40320};

inlineint inv_hash(char s[]){

    int ans=0;

    for(int i=0;s[i];i++){

       int cnt=0;

       for(int j=i-1;j>=0;j--){

           if(num[j]>num[i])cnt++;

       }

       ans+=fac[i]*cnt;

    }

    return ans;

}

 

 

5.Java

 

//Java文件操作

import java.io.*;

publicclass Main {

    staticpublicvoid main(String args[]) throwsFileNotFoundException{

       System.setIn(new FileInputStream("file.in"));

       System.setOut(new PrintStream("file.out"));

       //类似于C++中的freopen的用法

    }

}

 

//Java换行符:%n

 

//Java快排

importjava.util.Arrays;

@SuppressWarnings("rawtypes")

class Node implements Comparable{

    inta;

   

    Node(int a){this.a=a;}

   

    @Override

    publicint compareTo(Object arg0){

       int a=((Node)arg0).a;

       returnthis.a-a;

    }

   

    @Override

    public StringtoString(){

       return""+a;

    }

}

 

publicclass Main{

    publicstaticvoid main(String[] args){

       Node[] node=new Node[100];

       //赋值

       Arrays.sort(node);

       System.out.println(Arrays.toString(node));

    }

}

 

 

6. 输入输出外挂

 

//布尔型输入

inlinevoid in(bool& num){

    char in=getchar();

    while(in<=32) in=getchar();

    num=(in=='1');

}

 

//无符号整型输入

inlinevoid in(int &num){

    charin=getchar();

    while(!isdigit(in))in=getchar();

    num=in-'0';

    in=getchar();

    while(isdigit(in)){

       num*=10; num+=in-'0';

       in=getchar();

    }

}

 

//有符号整型输入

inlinevoid in(int &num){

    charin=getchar();

    bool IsN=false;

    while(in!='-'&&!isdigit(in))in=getchar();

    if(in=='-'){ IsN=true;num=0; }

    else num=in-'0';

    in=getchar();

    while(isdigit(in)){

       num*=10;num+=in-'0';

       in=getchar();

    }

    if(IsN) num=-num;

}

 

//无符号浮点输入

inlinevoid in(double &num){

    char in; double dec=0.1;

    bool isd=false;

    do{ in=getchar();

    }while(in!='.'&&!isdigit(in));

    if(in=='.') { isd=true;num=0; }

    else num=in-'0';

    if(!isd){

       in=getchar();

       while(isdigit(in)){

           num*=10;num+=in-'0';

           in=getchar();

       }

    }

    if(in=='.'){

       in=getchar();

       while(isdigit(in)){

           num+=dec*(in-'0');dec*=0.1;

           in=getchar();

       }

    }

}

 

//整数输出

inlinevoid out(int num){

    if(num<0){ putchar('-');num=-num; }

    if(num==0){ putchar('0');return; }

    char str[20];

    int bas=0;

    for(;num;num/=10)str[bas++]=num%10+'0';

    while(bas--)putchar(str[bas]);

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值