第一个C++程序 常规操作,先来打印Hello World
1 2 3 4 5 6 7 8 9 #include <iostream> using namespace std;int main () { cout << "Hello World" << endl; system ("pause" ); return 0 ; }
可以发现,和Python
相比,C++
的代码量要大得多
(Python的print("Hello World")
估计是最简单的了)
注释 Visul Studio中,Ctrl + /
可以快速注释,这个和Jetbrains
的IDE的快捷键一样
单行注释
多行注释
变量 1 2 3 4 5 6 7 8 9 10 #include <iostream> using namespace std;int main () { int a = 10 ; cout << a << endl; system ("pause" ); return 0 ; }
常量 用#define定义常量 1 2 3 4 5 6 7 8 9 10 11 #include <iostream> using namespace std;#define Day 7 int main () { cout << Day << endl; system ("pause" ); return 0 ; }
用const修饰变量,将其定义为常量 1 2 3 4 5 6 7 8 9 10 #include <iostream> using namespace std;int main () { const int month = 12 ; cout << month << endl; system ("pause" ); return 0 ; }
关键字 略
命名规范 字母大小写A-Z a-z
、数字1-9
和下划线_
,其中不能以数字开头(参见其他语言)
数据类型 整型 分短整型
、整型
、长整型
、长长整型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std;int main () { int a = 10 ; cout << a << "这是整型" << endl; short b =10 ; cout << b << "这是短整型" << endl; long c = 10 ; cout << c << "这是长整型" << endl; long long d = 10 ; cout << d << "这是长长整型" << endl; system ("pause" ); return 0 ; }
sizeof关键字 用于返回数据类型所占内存空间
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <iostream> using namespace std;int main () { cout << "int所占内存空间为:" << sizeof (int ) << endl; cout << "short所占内存空间为:" << sizeof (short ) << endl; cout << "long所占内存空间为:" << sizeof (long ) << endl; cout << "long long所占内存空间为:" << sizeof (long long ) << endl; system ("pause" ); return 0 ; }
实型 分为单精度浮点数
和双精度浮点数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <iostream> using namespace std;int main () { float a = 3.14f ; cout << a << "这是单精度浮点型" << endl; double b = 3.1415926 ; cout << b << "这是双精度浮点型" << endl; float f = 3e2 ; float f2 = 3e-2 ; cout << f << endl; cout << f2 << endl; system ("pause" ); return 0 ; }
字符型 有别于字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std;int main () { char a = 'a' ; cout << "这是字符型" << a << endl; cout << (int )a << endl; cout << "字符型变量大小" << sizeof (a) << endl; system ("pause" ); return 0 ; }
转义字符 1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream> using namespace std;int main () { cout << "123 \n" << endl; system ("pause" ); return 0 ; }
字符串 两种风格的字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std;int main () { char str1[] = "abc" ; cout << "C风格字符串" << str1 << endl; string str2 = "abcdefg" ; cout << "C++风格字符串" << str2 << endl; system ("pause" ); return 0 ; }
布尔值 0
为false,其余为true(1
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std;int main () { bool flag = true ; cout << flag << endl; flag = false ; cout << flag << endl; cout << sizeof (flag) << endl; system ("pause" ); return 0 ; }
数据输入 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <iostream> using namespace std;int main () { int a = 0 ; cout << "input an int" << endl; cin >> a; cout << "a is " << a << endl; float f = 3.14 ; cout << "input a float" << endl; cin >> f; cout << "f is " << f << endl; system ("pause" ); return 0 ; }
运算符 加减乘除 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <iostream> using namespace std;int main () { int a = 2 ; int b = 21 ; cout << "a + b = " << a + b << endl; cout << "a - b = " << a - b << endl; cout << "a / b = " << (float )a / (float )b << endl; cout << "a * b = " << a * b << endl; cout << "a % b = " << a % b << endl; system ("pause" ); return 0 ; }
取模运算 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <iostream> using namespace std;int main () { int a = 2 ; int b = 21 ; cout << "a % b = " << a % b << endl; system ("pause" ); return 0 ; }
自增、自减 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std;int main () { int a = 10 ; ++a; cout << "a = " << a << endl; int b = 100 ; b++; cout << "b = " << b << endl; system ("pause" ); return 0 ; }
赋值运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <iostream> using namespace std;int main () { int a = 100 ; a = 102 ; cout << a << endl; a = 100 ; a += 10 ; cout << a << endl; a = 100 ; a -= 10 ; cout << a << endl; a = 100 ; a /= 10 ; cout << a << endl; a = 100 ; a *= 10 ; cout << a << endl; a = 100 ; a %= 10 ; cout << a << endl; system ("pause" ); return 0 ; }
比较运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std;int main () { int a = 10 ; int b = 20 ; cout << (a == b) << endl; cout << (a != b) << endl; cout << (a < b) << endl; cout << (a > b) << endl; system ("pause" ); return 0 ; }
逻辑非、与、或运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std;int main () { int a = 10 ; int b = 20 ; cout << !(a == b) << endl; cout << (a && b) << endl; cout << (a || b) << endl; system ("pause" ); return 0 ; }
流程控制 选择结构 单if语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <iostream> using namespace std;int main () { int a = 0 ; cout << "input a sorce:" << endl; cin >> a; if (a > 600 ) { cout << "success" << endl; } else { cout << "sorry" << endl; } system ("pause" ); return 0 ; }
重复多行可得多行if语句
多if语句 这里的代码是多条件if语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <iostream> using namespace std;int main () { int a = 0 ; cout << "input a sorce:" << endl; cin >> a; if (a > 600 ) { cout << "1" << endl; } else if (a > 500 ) { cout << "2" << endl; } else { cout << "3" << endl; } system ("pause" ); return 0 ; }
嵌套if语句 略
三目运算符(三元运算符) <表达式> ? <表达式为真,则运行该语句> : <表达式为假,则运行该语句>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> using namespace std;int main () { int a = 10 ; int b = 20 ; int c = 30 ; c = a > b ? a : b; cout << c << endl; (a > b ? a : b) = 100 system ("pause" ); return 0 ; }
switch语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <iostream> using namespace std;int main () { int a = 0 ; cout << "input a num:" << endl; cin >> a; switch (a) { case 1 : cout << "small" << endl; break ; case 2 : cout << "middle" << endl; break ; case 3 : cout << "big" << endl; break ; default : cout << "unknow" << endl; break ; } system ("pause" ); return 0 ; }
循环结构 while 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <iostream> using namespace std;int main () { int a = 0 ; while (a < 10 ) { cout << "a = " << a << endl; a++; } system ("pause" ); return 0 ; }
案例-猜数字 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <iostream> using namespace std;int main () { srand ((unsigned int )time (NULL )); int a = rand ()%100 ; int b = 0 ; cout << "input a num (0 ~ 99)" << endl; cin >> b; while (a != b) { if (a > b) { cout << "Too Small" << endl; cout << "input a num (0 ~ 99)" << endl; cin >> b; } else { cout << "Too Big" << endl; cout << "input a num (0 ~ 99)" << endl; cin >> b; } } cout << "Correct!" << endl; system ("pause" ); return 0 ; }
do…while语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <iostream> using namespace std;int main () { int num = 0 ; do { cout << num << endl; num++; } while (num < 10 ); system ("pause" ); return 0 ; }
案例-水仙花数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <iostream> using namespace std;int main () { int num = 100 ; do { int a = num /100 ; int b = num / 10 % 10 ; int c = num % 10 ; if ((a*a*a + b*b*b + c*c*c) == num) { cout << num << endl; } num++; } while (num < 1000 ) system ("pause" ); return 0 ; }
for循环 1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream> using namespace std;int main () { for (int i = 0 ; i < 10 ; i++) { cout << i << endl; } system ("pause" ); return 0 ; }
三目运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std;int main () { int a = 10 ; int b = 20 ; int c = 30 ; c = a > b ? a : b; cout << c << endl; (a > b ? a : b) = 100 ; cout << b << endl; system ("pause" ); return 0 ; }
案例-拍桌子游戏 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> using namespace std;int main () { for (int i = 1 ; i <= 100 ; i++) { if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7 ) { cout << "敲桌子!" << endl; } else { cout << i << endl; } } system ("pause" ); return 0 ; }
嵌套循环 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std;int main () { for (int j = 0 ; j < 10 ; j++) { for (int i = 0 ; i < 10 ; i++) { cout << "* " ; } cout << endl; } system ("pause" ); return 0 ; }
案例-乘法口诀表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std;int main () { for (int i = 1 ; i < 10 ; i++) { for (int j = 1 ; j <= i; j++) { cout << j << " * " << i << " = " << i * j << "\t" ; } cout << endl; } system ("pause" ); return 0 ; }
break 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <iostream> using namespace std;int main () { int a = 0 ; cout << "input a num" << endl; cin >> a; switch (a) { case 1 : cout << "easy" << endl; break ; case 2 : cout << "middle" << endl; break ; default : cout << "difficult" << endl; break ; } for (int i = 0 ; i < 10 ; i++) { if (i == 5 ) { break ; } cout << i << endl; } system ("pause" ); return 0 ; }
continue 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std;int main () { for (int i = 0 ; i < 10 ; i++) { if (i == 5 ) { continue ; } cout << i << endl; } system ("pause" ); return 0 ; }
goto 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std;int main () { cout << 1 << endl; goto FLAG; cout << 2 << endl; cout << 3 << endl; FLAG: cout << 4 << endl; system ("pause" ); return 0 ; }
数组 一维数组 数组定义 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <iostream> using namespace std;int main () { int a[5 ]; a[0 ] = 1 ; a[1 ] = 2 ; a[2 ] = 3 ; a[3 ] = 3 ; a[4 ] = 6 ; for (int i = 0 ; i < 5 ; i++) { cout << a[i] << endl; } int b[5 ] = {1 ,2 ,40 }; for (int i = 0 ; i < 5 ; i++) { cout << b[i] << endl; } int c[] = {1 ,2 ,40 }; system ("pause" ); return 0 ; }
数组内存地址 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std;int main () { int a[] = {1 ,2 ,3 ,4 ,5 }; cout << sizeof (a) << endl; cout << sizeof (a[0 ]) << endl; cout << a << endl; cout << &a[0 ] << endl; system ("pause" ); return 0 ; }
案例-寻找最大值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> using namespace std;int main () { int a[5 ] = {100 ,200 ,350 ,250 ,400 }; int m = 0 ; for (int i = 0 ; i < 5 ; i++) { if (a[i] > m) { m = a[i]; } } cout << m << endl; system ("pause" ); return 0 ; }
案例-数组逆置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <iostream> using namespace std;int main () { int a[5 ] = {1 ,3 ,4 ,5 ,2 }; for (int i = 0 ; i < 5 ; i++) { cout << a[i]; } cout << endl; int start = 0 ; int end = sizeof (a) / sizeof (a[0 ]) - 1 ; int temp = 0 ; while (start < end) { temp = a[start]; a[start] = a[end]; a[end] = temp; start++; end--; } for (int i = 0 ; i < 5 ; i++) { cout << a[i]; } cout << endl; system ("pause" ); return 0 ; }
案例-冒泡排序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <iostream> using namespace std;int main () { int a[10 ] = {4 ,2 ,5 ,1 ,0 ,7 ,9 ,8 ,3 ,6 }; for (int i = 0 ; i < 10 ; i++) { cout << a[i]; } cout << endl; for (int i = 0 ; i < 10 - 1 ; i++) { for (int j = 0 ; j < 10 - 1 - i; j++) { int temp = a[j]; if (temp > a[j+1 ]) { a[j] = a[j+1 ]; a[j+1 ] = temp; } } } for (int i = 0 ; i < 10 ; i++) { cout << a[i]; } cout << endl; system ("pause" ); return 0 ; }
二维数组(多维数组) 数组定义 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <iostream> using namespace std;int main () { int arr[2 ][3 ] = {{1 ,2 ,3 },{4 ,5 ,6 }}; for (int i = 0 ; i < 2 ; i++){ for (int j = 0 ; j < 3 ; j++){ cout << arr[i][j] << " " ; } } cout << endl; int arr1[2 ][3 ] = {1 ,2 ,3 ,4 ,5 ,6 }; for (int i = 0 ; i < 2 ; i++){ for (int j = 0 ; j < 3 ; j++){ cout << arr1[i][j] << " " ; } } cout << endl; int arr2[][3 ] = {1 ,2 ,3 ,4 ,5 ,6 }; for (int i = 0 ; i < 2 ; i++){ for (int j = 0 ; j < 3 ; j++){ cout << arr2[i][j] << " " ; } } cout << endl; system ("pause" ); return 0 ; }
数组内存地址 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <iostream> using namespace std;int main () { int arr[2 ][3 ] = {1 ,2 ,3 ,4 ,5 ,6 }; cout << sizeof (arr) << endl; cout << arr << endl; cout << arr[0 ] << endl; cout << sizeof (arr) / sizeof (arr[0 ]) << endl; system ("pause" ); return 0 ; }
函数 函数定义、调用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <iostream> using namespace std;int add (int a,int b) { int sum = a + b; return sum; } int main () { int num1 = 5 ; int num2 = 10 ; int s = add (num1,num2); cout << "a + b = " << s << endl; system ("pause" ); return 0 ; }
分文件编写函数
1、创建<函数名>.h的头文件
2、创建<函数名>.cpp的源文件
3、头文件声明函数
4、源文件函数定义
头文件
1 2 3 4 #include <iostream> using namespace std;void swap (int a,int b) ;
函数定义
1 2 3 4 5 6 7 8 9 10 #include "swap.h" void swap (int a,int b) { int temp = a; a = b; b = temp; cout << a << endl; cout << b << endl; }
代码调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> #include "swap.h" using namespace std;int main () { int num1 = 5 ; int num2 = 20 ; cout << num1 << endl; cout << num2 << endl; swap (num1,num2); system ("pause" ); return 0 ; }