本文转载自:https://github.com/AnkerLeng/Cpp-0-1-Resource.git
视频见:https://www.bilibili.com/video/av41559729
编写一个C++程序总共分为4个步骤
Visual Studio是用来编写C++程序的主要工具,先将它打开。
右键源文件,选择添加 -> 新建项。
给C++文件起个名称,然后点击添加即可。
#include
using namespace std;
int main() {
cout << "Hello world" << endl;
system("pause"); // 按任意键继续
return 0; // 返回正常退出值
}
作用:在代码中加一些说明和解释,方便自己或其他程序员阅读代码
两种格式
1、单行注释:// 描述信息
ctrl+k+c
2、多行注释: /* 描述信息 */
/* main是一个程序的入口,每个程序都有一个main函数,有且仅有一个*/
提示:编译器在编译代码时,会忽略注释的内容
visual studio源文件中只能有一个main函数
作用:给一段指定的内存空间起名,方便操作这段内存。
语法:数据类型 变量名 = 初始值;
示例:
#include
using namespace std;
int main() {
//变量的定义
//语法:数据类型 变量名 = 初始值
int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
注意: C++在创建变量时,必须给变量一个初始值,否则会报错。
每段内存都会有一个地址编号,十六进制,缺点是如果编号太多不方便记录。
作用:用于记录程序中不可更改的数据。
C++定义常量两种方式:
1、#define 宏常量: #define 常量名 常量值
2、const修饰的变量 const 数据类型 常量名 = 常量值;
示例:
//1、宏常量
#define day 7
int main() {
cout << "一周里总共有 " << day << " 天" << endl;
//day = 8; //报错,宏常量不可以修改
//2、const修饰变量
const int month = 12;
cout << "一年里总共有 " << month << " 个月份" << endl;
//month = 24; //报错,常量是不可以修改的
system("pause");
return 0;
}
作用: 关键字是C++中预先保留的单词(标识符)。
C++关键字如下:
asm | do | if | return | typedef |
---|---|---|---|---|
auto | double | inline | short | typeid |
bool | dynamic_cast | int | signed | typename |
break | else | long | sizeof | union |
case | enum | mutable | static | unsigned |
catch | explicit | namespace | static_cast | using |
char | export | new | struct | virtual |
class | extern | operator | switch | void |
const | false | private | template | volatile |
const_cast | float | protected | this | wchar_t |
continue | for | public | throw | while |
default | friend | register | true | |
delete | goto | reinterpret_cast | try |
提示:在给变量或者常量起名称时候,不要用C++的关键字,否则会产生歧义。
作用:C++规定给标识符(变量、常量)命名时,有一套自己的规则。
建议: 给标识符命名时,争取做到见名知意的效果,方便自己和他人的阅读
C++规定在创建一个变量或者常量时,必须要指定出相应的数据类型,否则无法给变量分配内存。
作用:整型变量表示的是整数类型的数据。
C++中能够表示整型的类型有以下几种方式,区别在于所占内存空间不同:
数据类型 | 占用空间 | 取值范围 |
---|---|---|
short(短整型) | 2字节 | ( − 2 15 ∼ 2 15 − 1 -2^{15} \sim 2^{15}-1 −215∼215−1) |
int(整型) | 4字节 | ( − 2 31 ∼ 2 31 − 1 -2^{31} \sim 2^{31}-1 −231∼231−1) |
long(长整形) | Windows为4字节, Linux为4字节(32位),8字节(64位) |
( − 2 31 ∼ 2 31 − 1 -2^{31} \sim 2^{31}-1 −231∼231−1) |
long long(长长整形) | 8字节 | ( − 2 63 ∼ 2 63 − 1 -2^{63} \sim 2^{63}-1 −263∼263−1) |
作用: 利用sizeof关键字可以统计数据类型所占内存大小(字节数)
语法: sizeof( 数据类型 / 变量)
或 sizeof 表达式
示例:
int main() {
cout << "short 类型所占内存空间为: " << sizeof(short) << endl;
cout << "int 类型所占内存空间为: " << sizeof(int) << endl;
cout << "long 类型所占内存空间为: " << sizeof(long) << endl;
cout << "long long 类型所占内存空间为: " << sizeof(long long) << endl;
system("pause");
return 0;
}
整型结论:short < int <= long <= long long
#include
using namespace std;
int main()
{
cout << "bool: " << sizeof(bool) << endl;
cout << "char: " << sizeof(char) << endl;
cout << "unsigned char: " << sizeof(unsigned char) << endl;
cout << "signed char: " << sizeof(signed char) << endl;
cout << "short int: " << sizeof(short int) << endl;
cout << "unsigned short int: " << sizeof(unsigned short int) << endl;
cout << "signed short int: " << sizeof(signed short int) << endl;
cout << "int: " << sizeof(int) << endl;
cout << "unsigned int: " << sizeof(unsigned int) << endl;
cout << "signed int: " << sizeof(signed int) << endl;
cout << "long int: " << sizeof(long int) << endl;
cout << "unsigned long int: " << sizeof(unsigned long int) << endl;
cout << "signed long int: " << sizeof(signed long int) << endl;
cout << "long long int: " << sizeof(long long int) << endl;
cout << "unsigned long long int: " << sizeof(unsigned long long int) << endl;
cout << "signed long long int: " << sizeof(signed long long int) << endl;
cout << "float: " << sizeof(float) << endl;
cout << "long float: " << sizeof(long float) << endl;
cout << "double: " << sizeof(double) << endl;
cout << "long double: " << sizeof(long double) << endl;
cout << "bool指针: " << sizeof(bool*) << endl;
cout << "char指针: " << sizeof(char*) << endl;
cout << "unsigned char指针: " << sizeof(unsigned char*) << endl;
cout << "signed char指针: " << sizeof(signed char*) << endl;
cout << "short int指针: " << sizeof(short int*) << endl;
cout << "unsigned short int指针: " << sizeof(unsigned short int*) << endl;
cout << "signed short int指针: " << sizeof(signed short int*) << endl;
cout << "int指针: " << sizeof(int*) << endl;
cout << "unsigned int指针: " << sizeof(unsigned int*) << endl;
cout << "signed int指针: " << sizeof(signed int*) << endl;
cout << "long int指针: " << sizeof(long int*) << endl;
cout << "unsigned long int指针: " << sizeof(unsigned long int*) << endl;
cout << "signed long int指针: " << sizeof(signed long int*) << endl;
cout << "long long int指针: " << sizeof(long long int*) << endl;
cout << "unsigned long long int指针: " << sizeof(unsigned long long int*) << endl;
cout << "signed long long int指针: " << sizeof(signed long long int*) << endl;
cout << "float指针: " << sizeof(float*) << endl;
cout << "double指针: " << sizeof(double*) << endl;
system("pause");
return 0;
}
//32位输出结果
bool: 1
char: 1
unsigned char: 1
signed char: 1
short int: 2
unsigned short int: 2
signed short int: 2
int: 4
unsigned int: 4
signed int: 4
long int: 4
unsigned long int: 4
signed long int: 4
long long int: 8
unsigned long long int: 8
signed long long int: 8
float: 4
long float: 8
double: 8
long double: 8
bool指针: 4
char指针: 4
unsigned char指针: 4
signed char指针: 4
short int指针: 4
unsigned short int指针: 4
signed short int指针: 4
int指针: 4
unsigned int指针: 4
signed int指针: 4
long int指针: 4
unsigned long int指针: 4
signed long int指针: 4
long long int指针: 4
unsigned long long int指针: 4
signed long long int指针: 4
float指针: 4
double指针: 4
//64位输出结果
bool: 1
char: 1
unsigned char: 1
signed char: 1
short int: 2
unsigned short int: 2
signed short int: 2
int: 4
unsigned int: 4
signed int: 4
long int: 4
unsigned long int: 4
signed long int: 4
long long int: 8
unsigned long long int: 8
signed long long int: 8
float: 4
long float: 8
double: 8
long double: 8
bool指针: 8
char指针: 8
unsigned char指针: 8
signed char指针: 8
short int指针: 8
unsigned short int指针: 8
signed short int指针: 8
int指针: 8
unsigned int指针: 8
signed int指针: 8
long int指针: 8
unsigned long int指针: 8
signed long int指针: 8
long long int指针: 8
unsigned long long int指针: 8
signed long long int指针: 8
float指针: 8
double指针: 8
结论:
作用:用于表示小数
浮点型变量分为两种:
两者的区别在于表示的有效数字(包括整数和小数)范围不同。
数据类型 | 占用空间 | 有效数字范围 |
---|---|---|
float | 4字节 | 7位有效数字 |
double | 8字节 | 15~16位有效数字 |
示例:
int main() {
float f1 = 3.14f;
// f 表示“float”,编译器默认double,不加“f”表示这个步骤需要将双精度转换为单精度,加“f”表示定义一个单精度
double d1 = 3.14;
cout << f1 << endl;
cout << d1<< endl;
cout << "float sizeof = " << sizeof(f1) << endl;
cout << "double sizeof = " << sizeof(d1) << endl;
//科学计数法
float f2 = 3e2; // 3 * 10 ^ 2
cout << "f2 = " << f2 << endl;
float f3 = 3e-2; // 3 * 0.1 ^ 2
cout << "f3 = " << f3 << endl;
system("pause");
return 0;
}
默认情况下,输出一个小数,系统最多输出6位有效数字。
作用: 字符型变量用于显示单个字符
语法:char ch = 'a';
注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号
注意2:单引号内只能有一个字符,不可以是字符串
示例:
int main() {
char ch = 'a';
cout << ch << endl;
cout << sizeof(char) << endl;
//ch = "abcde"; //错误,不可以用双引号
//ch = 'abcde'; //错误,单引号内只能引用一个字符
cout << (int)ch << endl; //查看字符a对应的ASCII码
ch = 97; //可以直接用ASCII给字符型变量赋值
cout << ch << endl;
system("pause");
return 0;
}
ASCII码表格:
ASCII值 | 控制字符 | ASCII值 | 字符 | ASCII值 | 字符 | ASCII值 | 字符 |
---|---|---|---|---|---|---|---|
0 | NUT | 32 | (space) | 64 | @ | 96 | 、 |
1 | SOH | 33 | ! | 65 | A | 97 | a |
2 | STX | 34 | " | 66 | B | 98 | b |
3 | ETX | 35 | # | 67 | C | 99 | c |
4 | EOT | 36 | $ | 68 | D | 100 | d |
5 | ENQ | 37 | % | 69 | E | 101 | e |
6 | ACK | 38 | & | 70 | F | 102 | f |
7 | BEL | 39 | , | 71 | G | 103 | g |
8 | BS | 40 | ( | 72 | H | 104 | h |
9 | HT | 41 | ) | 73 | I | 105 | i |
10 | LF | 42 | * | 74 | J | 106 | j |
11 | VT | 43 | + | 75 | K | 107 | k |
12 | FF | 44 | , | 76 | L | 108 | l |
13 | CR | 45 | - | 77 | M | 109 | m |
14 | SO | 46 | . | 78 | N | 110 | n |
15 | SI | 47 | / | 79 | O | 111 | o |
16 | DLE | 48 | 0 | 80 | P | 112 | p |
17 | DCI | 49 | 1 | 81 | Q | 113 | q |
18 | DC2 | 50 | 2 | 82 | R | 114 | r |
19 | DC3 | 51 | 3 | 83 | S | 115 | s |
20 | DC4 | 52 | 4 | 84 | T | 116 | t |
21 | NAK | 53 | 5 | 85 | U | 117 | u |
22 | SYN | 54 | 6 | 86 | V | 118 | v |
23 | TB | 55 | 7 | 87 | W | 119 | w |
24 | CAN | 56 | 8 | 88 | X | 120 | x |
25 | EM | 57 | 9 | 89 | Y | 121 | y |
26 | SUB | 58 | : | 90 | Z | 122 | z |
27 | ESC | 59 | ; | 91 | [ | 123 | { |
28 | FS | 60 | < | 92 | / | 124 | | |
29 | GS | 61 | = | 93 | ] | 125 | } |
30 | RS | 62 | > | 94 | ^ | 126 | ` |
31 | US | 63 | ? | 95 | _ | 127 | DEL |
ASCII 码大致由以下两部分组成:
作用: 用于表示一些不能显示出来的ASCII字符
现阶段我们常用的转义字符有: \n \\ \t
转义字符 | 含义 | ASCII码值(十进制) |
---|---|---|
\a | 警报 | 007 |
\b | 退格(BS) ,将当前位置移到前一列 | 008 |
\f | 换页(FF),将当前位置移到下页开头 | 012 |
\n | 换行(LF) ,将当前位置移到下一行开头 | 010 |
\r | 回车(CR) ,将当前位置移到本行开头 | 013 |
\t | 水平制表(HT) (跳到下一个TAB位置) | 009 |
\v | 垂直制表(VT) | 011 |
\\ | 代表一个反斜线字符"\" | 092 |
’ | 代表一个单引号(撇号)字符 | 039 |
" | 代表一个双引号字符 | 034 |
? | 代表一个问号 | 063 |
\0 | 数字0 | 000 |
\ddd | 8进制转义字符,d范围0~7 | 3位8进制 |
\xhh | 16进制转义字符,h范围0~9,a~f,A~F | 3位16进制 |
示例:
int main() {
cout << "\\" << endl;
cout << "\tHello" << endl;
cout << "\n" << endl;
system("pause");
return 0;
}
作用:用于表示一串字符
两种风格
char 变量名[] = "字符串值"
示例:
int main() {
char str1[] = "hello world";
cout << str1 << endl;
system("pause");
return 0;
}
注意:C风格的字符串带有中括号,用双引号括起来
string 变量名 = "字符串值"
示例:
int main() {
string str = "hello world";
cout << str << endl;
system("pause");
return 0;
}
注意:C++风格字符串,需要加入头文件 #include
作用: 布尔数据类型代表真或假的值
bool类型只有两个值:
bool类型占1个字节大小
示例:
int main() {
bool flag = true;
cout << flag << endl; // 1
flag = false;
cout << flag << endl; // 0
cout << "size of bool = " << sizeof(bool) << endl; //1
system("pause");
return 0;
}
作用:用于从键盘获取数据
关键字: cin
语法: cin >> 变量
示例:
int main(){
//整型输入
int a = 0;
cout << "请输入整型变量:" << endl;
cin >> a;
cout << a << endl;
//浮点型输入
double d = 0;
cout << "请输入浮点型变量:" << endl;
cin >> d;
cout << d << endl;
//字符型输入
char ch = 0;
cout << "请输入字符型变量:" << endl;
cin >> ch;
cout << ch << endl;
//字符串型输入
string str = "hello";
cout << "请输入字符串型变量:" << endl;
cin >> str;
cout << str << endl;
//布尔类型输入
bool flag = true;
cout << "请输入布尔型变量:" << endl;
cin >> flag; // bool类型只要值不为0都为真
cout << flag << endl;
system("pause");
return EXIT_SUCCESS;
}
作用: 用于执行代码的运算
本章主要讲解以下几类运算符:
运算符类型 | 作用 |
---|---|
算术运算符 | 用于处理四则运算 |
赋值运算符 | 用于将表达式的值赋给变量 |
比较运算符 | 用于表达式的比较,并返回一个真值或假值 |
逻辑运算符 | 用于根据表达式的值返回真值或假值 |
作用:用于处理四则运算
算术运算符包括以下符号:
运算符 | 术语 | 示例 | 结果 |
---|---|---|---|
+ | 正号 | +3 | 3 |
- | 负号 | -3 | -3 |
+ | 加 | 10 + 5 | 15 |
- | 减 | 10 - 5 | 5 |
* | 乘 | 10 * 5 | 50 |
/ | 除 | 10 / 5 | 2 |
% | 取模(取余) | 10 % 3 | 1 |
++ | 前置递增 | a=2; b=++a; | a=3; b=3; |
++ | 后置递增 | a=2; b=a++; | a=3; b=2; |
– | 前置递减 | a=2; b=–a; | a=1; b=1; |
– | 后置递减 | a=2; b=a–; | a=1; b=2; |
示例1:
//加减乘除
int main() {
int a1 = 10;
int b1 = 3;
cout << a1 + b1 << endl;
cout << a1 - b1 << endl;
cout << a1 * b1 << endl;
cout << a1 / b1 << endl; // 结果为 3,两个整数相除结果依然是整数
int a2 = 10;
int b2 = 20;
cout << a2 / b2 << endl;
int a3 = 10;
int b3 = 0;
//cout << a3 / b3 << endl; //报错,除数不可以为0
//两个小数可以相除
double d1 = 0.5;
double d2 = 0.22;
cout << d1 / d2 << endl; // 运算的结果为小数
system("pause");
return 0;
}
总结: 在除法运算中,除数不能为0
示例2:
//取模
int main() {
int a1 = 10;
int b1 = 3;
cout << 10 % 3 << endl; // 1
int a2 = 10;
int b2 = 20;
cout << a2 % b2 << endl; // 10
int a3 = 10;
int b3 = 0;
//cout << a3 % b3 << endl; // 取模运算时,除数也不能为0
//两个小数不可以取模
double d1 = 3.14;
double d2 = 1.1;
//cout << d1 % d2 << endl;
system("pause");
return 0;
}
总结: 只有整型变量可以进行取模运算
示例3:
//递增
int main() {
//后置递增
int a = 10;
a++; //等价于a = a + 1
cout << a << endl; // 11
//前置递增
int b = 10;
++b;
cout << b << endl; // 11
//区别
//前置递增先对变量进行++,再计算表达式
int a2 = 10;
int b2 = ++a2 * 10;
cout << b2 << endl; // 110,a2 = 11
//后置递增先计算表达式,后对变量进行++
int a3 = 10;
int b3 = a3++ * 10;
cout << b3 << endl; // 100,a3 = 11
system("pause");
return 0;
}
总结: 前置递增先对变量进行++,再计算表达式,后置递增相反
作用: 用于将表达式的值赋给变量
赋值运算符包括以下几个符号:
运算符 | 术语 | 示例 | 结果 |
---|---|---|---|
= | 赋值 | a=2; b=3; | a=2; b=3; |
+= | 加等于 | a=0; a+=2; | a=2; |
-= | 减等于 | a=5; a-=3; | a=2; |
*= | 乘等于 | a=2; a*=2; | a=4; |
/= | 除等于 | a=4; a/=2; | a=2; |
%= | 模等于 | a=3; a%2; | a=1; |
示例:
int main() {
//赋值运算符
// =
int a = 10;
a = 100;
cout << "a = " << a << endl;
// +=
a = 10;
a += 2; // a = a + 2;
cout << "a = " << a << endl;
// -=
a = 10;
a -= 2; // a = a - 2
cout << "a = " << a << endl;
// *=
a = 10;
a *= 2; // a = a * 2
cout << "a = " << a << endl;
// /=
a = 10;
a /= 2; // a = a / 2;
cout << "a = " << a << endl;
// %=
a = 10;
a %= 2; // a = a % 2;
cout << "a = " << a << endl;
system("pause");
return 0;
}
作用: 用于表达式的比较,并返回一个真值或假值
比较运算符有以下符号:
运算符 | 术语 | 示例 | 结果 |
---|---|---|---|
== | 相等于 | 4 == 3 | 0 |
!= | 不等于 | 4 != 3 | 1 |
< | 小于 | 4 < 3 | 0 |
> | 大于 | 4 > 3 | 1 |
<= | 小于等于 | 4 <= 3 | 0 |
>= | 大于等于 | 4 >= 1 | 1 |
示例:
int main() {
int a = 10;
int b = 20;
cout << (a == b) << endl; // 0 ,有优先级
cout << (a != b) << endl; // 1
cout << (a > b) << endl; // 0
cout << (a < b) << endl; // 1
cout << (a >= b) << endl; // 0
cout << (a <= b) << endl; // 1
system("pause");
return 0;
}
注意:C和C++ 语言的比较运算中, “真”用数字“1”来表示, “假”用数字“0”来表示。
作用: 用于根据表达式的值返回真值或假值
逻辑运算符有以下符号:
运算符 | 术语 | 示例 | 结果 |
---|---|---|---|
! | 非 | !a | 如果a为假,则!a为真; 如果a为真,则!a为假。 |
&& | 与 | a && b | 如果a和b都为真,则结果为真,否则为假。 |
|| | 或 | a || b | 如果a和b有一个为真,则结果为真,二者都为假时,结果为假。 |
示例1: 逻辑非
//逻辑运算符 --- 非
int main() {
int a = 10;
cout << !a << endl; // 0
cout << !!a << endl; // 1
system("pause");
return 0;
}
总结:逻辑非运算符总结: 真变假,假变真
示例2: 逻辑与
//逻辑运算符 --- 与
int main() {
int a = 10;
int b = 10;
cout << (a && b) << endl; // 1
a = 10;
b = 0;
cout << (a && b) << endl; // 0
a = 0;
b = 0;
cout << (a && b) << endl; // 0
system("pause");
return 0;
}
总结:逻辑与运算符总结: 同真为真,其余为假
示例3: 逻辑或
//逻辑运算符 --- 或
int main() {
int a = 10;
int b = 10;
cout << (a || b) << endl; // 1
a = 10;
b = 0;
cout << (a || b) << endl; // 1
a = 0;
b = 0;
cout << (a || b) << endl; // 0
system("pause");
return 0;
}
逻辑或运算符总结: 同假为假,其余为真