C++常用头文件
标准C++头文件
using namespace std;
1. #include
算法类函数
函数 | 作用 |
---|
void sort (RandomAccessIterator first, RandomAccessIterator last)默认升序
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp)comp为自定义排序规则
#include
#include
using namespace std;
int main ()
{
//定义一个大小为10的数组
int arr[10] = {3, 5, 8, 2, 0, 9, 4, 7, 1, 6};
//对数组前5个元素进行排序
sort(arr, arr+5);
for(int i=0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
执行结果:0 2 3 5 8 9 4 7 1 6
#include
#include
using namespace std;
bool comp(int x, int y)
{
// 按升序进行排序
return x < y;
// 按降序进行排序
//return x > y;
}
int main ()
{
//定义一个大小为10的数组
int arr[10] = {3, 5, 8, 2, 0, 9, 4, 7, 1, 6};
//对数组前5个元素进行排序
sort(arr, arr+5, comp);
for(int i=0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
执行结果:0 2 3 5 8 9 4 7 1 6
2. #include
定义数据流输入输出
函数 | 作用 |
---|
cin>>数据输入流
cout<<数据输出流
3. #include
头文件cstdio/stdio.h是C/C++使用最频繁的文件,因为文件中包含很多常用的方法
函数 | 作用 |
---|
scanf( ) / printf( )格式化输入输出
getchar( ) / putchar( )输入/输出一个字符
gets( ) / puts( )输入/输出一个字符串
FILE* fopen( ) / fclose( )文件访问
#include
int main ()
{
FILE * pFile;
pFile = fopen ("myfile.txt","w");
if (pFile!=NULL)
{
fputs ("fopen example",pFile);
fclose (pFile);
}
return 0;
4. #include
数学函数
函数 | 作用 |
---|
int abs(int i)返回整型参数i的绝对值
double exp(double x)返回指数ex的值
double log(double x)返回对数logex的值
double log10(double x)返回log10x的值
double pow(double x,double y)返回xy的值
double pow10(int p)返回10p的值
double sqrt(double x)返回+√x的值
double ceil(double x)返回不小于x的最小整数
double floor(double x)返回不大于x的最大整数
void srand(unsigned seed)初始化随机数发生器
int rand( )产生一个随机数并返回这个数
double acos(double x)返回x的反余弦cos-1(x)值,x为弧度
double asin(double x)返回x的反正弦sin-1(x)值,x为弧度
double atan(double x)返回x的反正切tan-1(x)值,x为弧度
double cos(double x)返回x的余弦cos(x)值,x为弧度
double sin(double x)返回x的正弦sin(x)值,x为弧度
double tan(double x)返回x的正切tan(x)值,x为弧度
5. #include
字符处理
函数 | 作用 |
---|
isalpha()判断一个字符是否为字母, 如果是字母则返回非零, 否则返回0
islower()判断一个字符是否是小写字母,如果是小写字母则返回非零, 否则返回0
isupper()判断一个字符是否是大写字母,如果是大写字母则返回非零, 否则返回0
isalnum()判断一个字符是否为数字或字母,如果是则返回非零, 否则返回0
isblank(space和t)判断字符是否为空格或制表符
isspace(space、t、r、n)/
tolower()将大写字母转换为小写字母
toupper()将小写字母转换为大写字母
6. #include
字符串处理
7. #include
STL位集容器
8. #include
STL线性表容器
9. #include
STL动态数组容器
10. #include
评论列表(0条)