Cpp中的一些函数

刷PAT过程中使用到的一些函数和使用

stoi():把字符串变成数字
to_string():数字变字符串
substr():截取字符串部分
ceil()和floor():向上和下取整,返回为double类型
getline(cin, s,结束符):输入使用
c_str():输出string时用printf(“%s”,s.c_str())
printf("%.*d",b,x):把x前面添加b个0
roud(double x):四舍五入
memset和fill:都是初始化数组的时候使用,第一个速度快一点 ,但建议只赋为0或-1
strcmp(s1,s2):字符串的比较大小,返回-1,0,1
strcpy(s1,s2):字符串的复制
strcat(s1,s2):把s2拼接到s1后面
sscanf(str, “%d”, &n):把str中的内容以%d写到n中,例如:
char str[20] = "2018:3.14,hello";
sscanf(str,%d:%lf,%s”,&n,&b,str2);
sprintf():把结果输入到str中,例如:
char str[20];
sprintf(str,%d:%lf,%s”,2018,3.14,",hello");
//则str中内容为:"2018:3.14,hello";
reverse():字符串(vector等其他类似)以及数组的反转:
string s = "12345";
reverse(s.begin(),s.end());
//则s的结果为:"54321"

int s[3]={
    
    1,2,3};
reverse(s,s+3);
//则s的结果为:{3,2,1}
map本身是不能排序的,但是在声明时第三个参数加入结构类型即可
struct cmp{
    
    
    bool operator()(const int&a, const int&b){
    
    
        return a>b;
    }

};
map<int,int,cmp>ma;
ma[0] = 1;
ma[1] = 2;
ma[2] = 3;
ma[3] = 4;
for(auto it = ma.begin();it!=ma.end();it++)
	cout<<it->first<<" "<<it->second<<endl;
//则对map进行遍历时,从头到尾为的值为4-3-2-1
遇到不会的之后继续补充:(#^.^#)

猜你喜欢

转载自blog.csdn.net/qq_43567222/article/details/112918907