C++常用头文件:
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
1.#include <iostream>
using namespace std;
2.字符串的头文件
#include <string>
- 字符转整型 int t = n[0] - '0';
- 求字符串长度 str.length()
- 字符串转整型 int t = atoi(str);
- 整型转字符串 string str = itoa(t);
- 整型转字符 char ch = x + '0';
- 字符转整型 int x = ch - '0'; (A的asc码值为65,若想A对应1,则int a = 'A' - 64; 字符9转整型9:int a = '9' - '0'; )
- 查找函数 int position = str.find("PAAT"); (找得到显示子串在字符数组中的下标,找不到显示-1)
- 用cin读取字符串时,只能读到第一个空格处
- getline(cin,str);接收一个字符串,可以接收空格并输出
-
s.substr(pos, n) 截取s中从pos开始(包括0)的n个字符的子串
-
s.substr(pos) 截取s中从pos开始(包括0)到末尾的所有字符的子串
4.algorithm头文件
sort(a,a+10); //对数组进行由小到大的排序
/***********对数组进行由大到小的排序(char类型、结构体同样适用)************/
结构体排序:
按照sum降序排序;sum相同时,按照s1降序排序;s1也相同时,按照num升序排序
bool cmp(student t1,student t2)
{
if(t1.sum == t2.sum)
{
if(t1.s1 == t2.s1)
return t1.num < t2.num;
else
return t1.s1 > t2.s1;
}
else
return t1.sum > t2.sum;
}
bool cmp(int a,int b){
return a > b;
}
sort(a,a+10,cmp);
/******************************************************/
reverse(a,a+10); //对整型数组进行逆转
reverse(str.begin()+2, str.begin()+6); //对字符串进行逆转
fill( a, a+5, 233); //将a[0]~a[4]赋值为233
5.C++中使用cmath头文件
- sqrt(x)函数