C++快速回顾重点

本文是《算法笔记》胡凡、曾磊著 的精炼总结。用于博主本人的快速复习,具有极强主观性,请酌情学习。

一、C/C++语言基础

变量

  1. int占4字节,**范围大概$10^9$**内。
  2. long long占8字节,$10^9$外用long long。
  3. float占4字节,1位符号位、8位指数位、23位尾数位,有效精度6~7位
  4. double占8字节,1位符号位、11位指数位、52位尾数位,有效精度15~16位。尽量用double。
  5. char小写字母比大写字母ascll码大32
  6. char单个字符常量必须用单引号
  7. char[]字符串常量用双引号
  8. bool在整型转布尔时,非零为true,零为false

常量

  1. 宏定义末尾不加分号

    #define 标识符 常量
    
  2. 推荐使用const定义常量

    const 数据类型 变量名 = 常量
    
  3. 宏定义陷阱

    #include<iostream>
    using namespace std;
    #define CAL(x) (x * 2 + 1)
    int main(){
        int a = 1;
        printf("%d\n",CAL(a+1));
        return 0;
    }
    

    输出结果:

    4
    

    因为宏定义是直接将对应的部分替换,然后才进行编译和运行的。

    即上式CAL(a+1)=(a + 1 x 2 +1),而不是((a + 1) x 2 + 1)

运算符

  1. 除法运算符需要考虑(1)整型/整型=整型,直接舍去小数部分;(2)除数为0报错
  2. 取模运算符需要考虑(1)除数为0报错

scanf输入printf输出

  1. 对于double类型,scanf("%lf",%db);而printf("%f",db);
  2. 除%c外,scanf对其他格式符输入是以空白符(即空格、换行等)为结束判断标志的。
  3. scanf对于%c是可以读入空格和换行的。

typedef

  1. 作用是给复杂的数据类型起一个别名

    如:

    #include<cstdio>
    typedef long long LL;
    ...
    

math函数

  1. double fabs(double x)取绝对值
  2. double floor(double x)向下取整
  3. double ceil(double x)向上取整
  4. double pow(double r,double p)返回r^p
  5. double sqrt(double x)算术平方根
  6. double log(double x)
  7. double sin(double x)、double cos(double x)、double tan(double x)
  8. double asin(double x)、double acos(double x)、double atan(double x)
  9. double round(double x)四舍五入

数组

  1. 冒泡排序

  2. memset填充方法

    memset(数组名, , sizeof(数组名));
    

    如:

    ...
    int a[5]={1,2,3,4,5};
    memset(a,0,sizeof(a));
    ...
    

    执行完第3行后,a的值为

    0 0 0 0 0
    

字符数组

  1. 只有初始化是才能直接赋值整个字符串,在程序其他位置不允许这种操作。

  2. 对于字符数组,可以使用getchar/putchar进行单个字符的输入输出

    ...
    char str[5][5];
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            str[i][j]=getchar();
        }
        getchat();//把输入中每行末尾的换行符吸收掉
    }
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            putchar(str[i][j]);
        }
        putchar('\n');
    }
    ...
    
  3. 对于字符数组,可以使用gets/puts进行字符串的输入输出

    ...
    char str1[20];
    char str2[5][10];
    gets(str1);
    for(int i=0;i<3;i++){
    gets(str2[i]);
    }
    puts(str1);
    for(int i=0;i<3;i++){
    puts(str2[i]);
    }
    ...
    
  4. 在一维字符数组(或者二维字符数组的第二维)的末尾都有一个空字符\0,以表示存放的字符串的结尾

    (gets、scanf输入是自动添加\0,并占一位。而puts和printf就是通过识别\0作为字符串的结尾的)

  5. 关于\0的特别提醒:

    • 特别提醒1:结束符**\0的 ASCII 码为0**,即空字符NULL,占用一个字符位,因此开字符数组的时候千万要记得字符数组的长度一定要比实际存储字符串的长度至少多1。注意:int 型数组的末尾不需要加\0,只有char 型数组需要。还需要注意\0跟空格不是同一个东西,空格的ASCI码是32,切勿混淆。
    • 特别提醒2:如果不是使用scanf 函数的%s格式或 gets 函数输字符串(例如使用getchar),请一定要在输入的每个字符串后加入“\0”,否则 printf 和 puts前出字符串会因无法识别字符串末尾而输出一大堆乱码,如:
    ... 
    char str[15]; 
    for(int i=0;i<3;i++){ 
    	str[i]=getchar(); 
    } 
    puts(str); 
    ...
    

    输入如下:

    T^T
    

    结果如下:

    T^T腫?w?@
    

string.h头文件

  1. strlen()获得字符数组第一个\0前的字符的个数
  2. strcmp()返回两个字符串大小的比较结果(按字典顺序比较)
  3. strcpy()把字符串复制给另一个字符串(包括结束符\0)
  4. strcat()把一个字符串连接到另一个字符串后面

sscanf与sprintf

stdio.h头文件下

如果想要从屏幕输入int型变量n并将int型变量n输出到屏幕,写法是这样的:

scanf("%d",&n);
printf("%d",n);

而事实上,上面的写法其实可以表示成下面的样子,其中screen表示屏幕:

scanf(screen,"%d",&n);
printf(screen,"%d",n);

可以发现,scanf的输入其实是把screen的内容以“%d”的格式传输到n中(即从左至右),而printf的输出则是把n以"%d"的格式传输到screen上(即从右到左)。

而sscanf与sprintf把screen换成了字符数组:

sscanf(str,"%d",&n);
sprintf(str,"%d",n);

sscanf作用是把字符数组str中的内容以"%d"的格式写到n中

...
char str[100] = "123";
sscanf(str,"%d",&n);
printf("%d\n",n);
...

输出结果

123

sprintf作用是把n以"%d"的格式写到字符数组str中

...
char str[100];
sprintf(str,"%d",n);
printf("%s\n",str);
...

输出结果

233

事实上,sscanf与sprintf可以进行更加复杂的操作:

...
int n;
double db;
char str[100]="2048:3.14,hello",str2[100];
sscanf(str,"%d:%lf,%s",&n,&db,str2);
...
...
int n=12;
double db=3.1415;
char str[100],str2[100]="good";
sprintf(str,"%d:%.2f,%s",n,db,str2);
...

标题:C++快速回顾重点——C语言基础(一)
作者:Departure
地址:https://www.unreachablecity.club/articles/2023/04/16/1681658130979.html