1、书写一个hello world
#include<iostream>
using namespace std;
int main() {
cout << "hello world / C++" << endl;
system("pause");
return 0;
}
2、注释
// only one line 这是一个单行注释
/*
这是一个多行注释
*/
3、变量
#include<iostream>
using namespace std;
int main() {
// do something here
// varible: easier to control memoey space
// 变量创建:数据类型 变量名 = 变量的初始值
int a = 10;
cout << "a=" << a << endl;
system("pause");
return 0;
}
变量创建:数据类型 变量名 = 变量的初始值: int a = 10;
4、定义一个常量
#include<iostream>
using namespace std;
// 1、定义一个宏常量, 表示不可以修改, 末尾不要加分号
#define Day 7
int main() {
// 2、定义一个不可变量
const int month = 12;
cout << "一周一共有" << Day << "天" << endl;
cout << "一年一共有" << month << "月" << endl;
system("pause");
}
5、整型
#include<iostream>
using namespace std;
int main() {
/*
short: 2bits
int: 4bits
long: 4bits;
long long: 8bits;
超出最大显示范围,从下边界往前进行运算
*/
short num1 = 10;
int num2 = 10;
long num3 = 10;
long long num4 = 10;
cout << "number1 = " << num1 << endl;
cout << "number2 = " << num2 << endl;
cout << "number3 = " << num3 << endl;
cout << "number4 = " << num4 << endl;
return 0;
}
6、sizeof 关键字
#include<iostream>
using namespace std;
int main() {
/*
short: 2bits
int: 4bits
long: 4bits;
long long: 8bits;
超出最大显示范围,从下边界往前进行运算
*/
short num1 = 10;
int num2 = 10;
long num3 = 10;
long long num4 = 10;
cout << "number1 所占的字节为 : " << sizeof(num1) << endl;
cout << "number2 所占的字节为 : " << sizeof(num2) << endl;
cout << "number3 所占的字节为 : " << sizeof(num3) << endl;
cout << "number4 所占的字节为 : " << sizeof(num4) << endl;
return 0;
}
7、实型(浮点型)
#include<iostream>
using namespace std;
/*
float : 4bits : 可以有7位
double : 8bits : 可以有15 ~ 16位
*/
int main() {
// 不加 f 的话默认为double类型, 也就时先当作double类型处理,然后当作float处理
float f1 = 3.14f;
double f2 = 3.1415926;
cout << f1 << "///" << f2 << endl;
return 0;
}
8、字符型
#include<iostream>
using namespace std;
/*
char ch = 'a';
字符型只能用单引号
字符型只能储存一个字符
字符存储过程中实质是 ascii 字码编号
*/
int main() {
char ch = 'a';
cout << ch << "字符的大小:" << sizeof(ch) << endl;
cout << ch << "对应的ascii码为" << (int)ch << endl;
return 0;
}
9、转译字符
#include<iostream>
using namespace std;
/*
/t :制表
/n :换行
// : 输出 /
*/
int main() {
cout << "first line \nsecond line" << endl;
return 0;
}
10、字符串类型
#include<iostream>
using namespace std;
int main() {
// c style of string
char str1[] = "c style of string";
// c++ style of string
string str2 = "c++s tyle of string";
cout << "c style====> " << str1 << "\n" << "c++ style ====>" << str2 << endl;
return 0;
}
11、布尔数据类型
#include<iostream>
using namespace std;
/*
true // false
need 1 bites
*/
int main() {
// create boolean type
bool flag = true;
cout << flag << endl;
// check memery of boolean type
cout << sizeof(flag) << "字节" << endl;
return 0;
}
12、数据类型的输入
#include<iostream>
using namespace std;
#include<string>
int main() {
int a = 0;
cout << "the org value a = 0; change the value of a: " << endl;
cin >> a;
cout << "the new value of a is:" << a << endl;
string str1 = "abcde";
cout << str1 << endl;
cin >> str1;
cout << str1 << endl;
return 0;
}
13、运算符
#include<iostream>
using namespace std;
int main() {
int a = 10;
int b = 3;
cout << "两个整数相除依然是整数:" << b / a << endl;
cout << "两个小数相除依然是小数:" << (float)b / (float)a << endl;
cout << "取模运算:" << a % b << endl;
cout << "前置递增(运算前先进行+1):" << ++a << endl;
cout << "后置递增(运算后先进行+1):" << a++ << endl;
return 0;
}
14、三目运算符
#include<iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
int c = 0;
c = a > b ? a : b;
cout << "c output:" << c << endl;
// (a > b ? a : b) = 100; 将最大的变量的值修改为100
return 0;
}
15、do...while...语句
#include<iostream>
using namespace std;
int main() {
int a = 0;
do {
a++;
cout << "a的值为:" << a << endl;
} while (a < 10);
return 0;
}
16、实战乘法口诀表
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << i * j << " ";
}
cout << endl;
}
return 0;
}
17、goto 跳转语句
#include<iostream>
using namespace std;
int main() {
goto Flag;
cout << "不会执行" << endl;
Flag:
cout << "会执行" << endl;
return 0;
}
18、一维数组的定义
#include<iostream>
using namespace std;
int main() {
// number 1: 必须指定长度,第一种定义方式
int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
cout << arr[0] << endl;
// number 2, 第二种定义方式
int arr2[3] = { 10, 20, 30 };
for (int i = 0; i < 3; i++)
{
cout << arr[i] << endl;
}
// number 3: 可以不用指定长度, 第三种定义方式
int arr3[] = { 10, 20, 40, 60 };
return 0;
}
19、一维数组的简单操作
#include<iostream>
using namespace std;
int main() {
int arr[] = { 1, 2, 3, 5, 8 };
cout << "arr的内存空间的大小为:" << sizeof(arr) << endl;
cout << "第一个元素所占的内存空间为:" << sizeof(arr[0]) << endl;
cout << "数组中一共有元素:" << sizeof(arr) / sizeof(arr[0]) << endl;
// 查看数组的地址
cout << "数组中的首地址为:" << arr << "转化为十进制为:" << (int)arr << endl;
// 数组的首地址和第一个元素的地址相同
// 数组中相邻元素的地址相差4个字节
cout << "数组中第一个元素的地址为:" << &arr[0] << "地址的十进制为:"<< (int)&arr[1] << endl;
return 0;
}
20、二维数组的简单操作
#include<iostream>
using namespace std;
int main() {
// 1、定义 arr[row][col]
int arr[2][3];
arr[0][1] = 100;
cout << arr[0][1];
return 0;
}
21、排序算法:冒泡排序
#include<iostream>
using namespace std;
int main() {
int arr[] = { 3, 6, 4, 5, 7, 9, 1 };
int arr_len = sizeof(arr) / sizeof(arr[0]);
for (int i = arr_len; i > 0; i--) {
for (int j = 1; j < i; j++)
{
if (arr[j - 1] > arr[j]) {
int temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < arr_len; i++) {
cout << arr[i] << " ";
}
return 0;
}
22、二维数组简单操作
#include<iostream>
using namespace std;
int main() {
int arr[2][3] = {
{1, 2, 3},
{2, 3, 4}
};
cout << "二维数组占用的空间为:" << sizeof(arr) << endl;
cout << "二维数组占用的首空间为" << sizeof(arr[0]) << endl;
return 0;
}
23、函数的分文件编写
#include<iostream>
using namespace std;
// 实现之前先将该文件从生成中排除改为否
// 调用头文件中的函数
#include"swap_55.h"
// 实例见 swap_55 等文件
/*
1、创建.h的后缀名的头文件
2、创建.cpp为后缀名的源文件
3、在头文件中编写函数的声明
4、在源文件中先函数的定义
*/
// 函数的声明
// void swap(int a, int b);
// 函数的定义
/*
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
}
*/
int main() {
int a = 10;
int b = 20;
swap(a, b);
return 0;
}
24、指针的定义和使用
#include<iostream>
using namespace std;
// 简介通过地址查询, 如果知道地址为多少,也可以拿到这个数据
// 通过一个变量来保存另一个变量的地址
// 指针就是一个地址
int main() {
int a = 10;
// 定义指针语法: 数据类型 * 指针变量名
// 如何定义一个指针
int * p;
// 让指针记录a的地址
p = &a;
cout << "a的地址为:" << &a << endl;
cout << "指针p的值为:" << p << endl;
// 如何操作一个指针
// 通过解引用的方式来解指针的地址
// 指针前面加 * 表示解引用, 找到指针指向内存中的数据
// *p: 指针中内存中指向的数据
*p = 1000;
// 通过指针来间接访问内存中的地址来修改和访问
cout << "a=" << a << endl;
cout << "*p=" << *p << endl;
return 0;
}
25、指针所占用的内存空间
#include<iostream>
using namespace std;
// 简介通过地址查询, 如果知道地址为多少,也可以拿到这个数据
// 通过一个变量来保存另一个变量的地址
// 指针就是一个地址
int main() {
int a = 10;
// 定义指针语法: 数据类型 * 指针变量名
// 如何定义一个指针
int * p;
// 让指针记录a的地址
p = &a;
cout << "a的地址为:" << &a << endl;
cout << "指针p的值为:" << p << endl;
// 如何操作一个指针
// 通过解引用的方式来解指针的地址
// 指针前面加 * 表示解引用, 找到指针指向内存中的数据
// *p: 指针中内存中指向的数据
*p = 1000;
// 通过指针来间接访问内存中的地址来修改和访问
cout << "a=" << a << endl;
cout << "*p=" << *p << endl;
return 0;
}
26、空指针与野指针
#include<iostream>
using namespace std;
int main() {
// 指针变量指向内存中编号为0的空间
// 用途: 初始化指针变量
// 空指针的内存是不可以访问的
// 0 - 255 ; 不要随便访问
int* p = NULL;
cout << *p << endl;
// 野指针, 随便指向的系统中的一个地址, 这个是系统其他程序使用的内存地址
// 最好不要使用, 这个指向的是非法的内存空间
int* p1 = (int*)0x1100;
cout << *p << endl;
return 0;
}
27、const修饰指针
#include<iostream>
using namespace std;
int main() {
// const 修饰指针 ---> 常量指针 ---> const int* p = &a ---> 指针的指向可以修改;指向的地址的值不能修改
int a = 10;
const int* p = &a;
// 常量 ---------> const
// 指针 ---------> int*
// const 修饰常量 ---> 指针常量 ---> int* const p = &a ---> 指针的指向不可以修改, 指向的值可以修改
int* const p1 = &a;
// const 即修饰指针, 又修饰常量, 指针的指向和指针指向的值都不能修改
const int* const p2 = &a;
return 0;
}
28、指针和数组
#include<iostream>
using namespace std;
int main() {
// 指针和数组
// 利用指针访问数组中的元素
int arr[10] = { 1, 3, 10,4, 5,6,7,8,9 };
cout << "第一个元素为:" << arr[0] << endl;
int* p = arr;
cout << "利用指针访问的第一个值为:" << *p << endl;
// 将指针向后移动一位, 内存地址+4(表示移动了4个字节)
p++;
cout << "移动指针后的第一个元素:" << *p << endl;
return 0;
}
29、指针和函数
#include<iostream>
using namespace std;
void swap(int a, int b) {
int temp = b;
b = a;
a = temp;
// 在这里可以理解为变量为新开辟的一个空间
// 函数内部a的地址和函数外部a的地址不同
cout << "swap函数内部a的地址:" << &a << endl;
}
void swap2(int* p1, int* p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
// 在这里可以理解为函数在原来地址空间上进行的值交换
cout << "swap2函数内部a的地址:" << p1 << endl;
}
int main() {
// 指针和函数
// 1、值传递
int a = 10;
int b = 20;
swap(10, 20);
cout << "a=" << a << "函数外部a的地址:" << &a <<endl;
cout << "b=" << b << endl;
cout << "地址传递之后---》" << endl;
// 2、地址传递
swap2(&a, &b);
cout << "a=" << a << "函数外部a的地址:" << &a << endl;
cout << "b=" << b << endl;
return 0;
}
30、指针配合数组与函数
#include<iostream>
using namespace std;
void bubble_sort(int* arr, int arr_len) {
for (int i = arr_len; i > 0; i--) {
for (int j = 1; j < i; j++)
{
if (arr[j - 1] > arr[j]) {
int temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
}
int main() {
// 创建一个数组
int arr[] = { 10, 4, 6, 2, 7, 9, 8 };
// 创建函数, 实现一个冒泡排序
int arr_len = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, arr_len);
// 打印排序后的数组
for (int i = 0; i < arr_len; i++)
{
cout << arr[i] << " ";
}
return 0;
}
31、结构体的定义和使用
#include<iostream>
using namespace std;
// 用户自定义数据类型, 相当于 python类的一个构造函数, 或者 js的constructor()构造函数
// 1、创建学生的数据类型
struct Student
{
string name;
int age;
int score;
}s3;
// 这里的S3就是顺便创建的结构体变量
// 2、通过学生类型创建具体的学生
// 2-1: struct Student s1; 只是初始化实例
// 2-2: struct Student s2 = { ... }; 初始化实例的同时赋值
// 2-3: 顺便创建结构体变量
int main() {
// struct 关键字可以省略
Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 98;
cout << "name: " << s1.name << " age: " << s1.age << " score: " << s1.score << endl;
struct Student s2 = { "小明", 19, 99 };
cout << "name: " << s2.name << " age: " << s2.age << " score: " << s2.score << endl;
s3.name = "李四";
s3.age = 20;
s3.score = 97;
cout << "name: " << s3.name << " age: " << s3.age << " score: " << s3.score << endl;
return 0;
}
32、结构体数组
#include<iostream>
using namespace std;
// 用户自定义数据类型, 相当于 python类的一个构造函数, 或者 js的constructor()构造函数
// 1、创建学生的数据类型
struct Student
{
string name;
int age;
int score;
}s3;
// 这里的S3就是顺便创建的结构体变量
// 2、通过学生类型创建具体的学生
// 2-1: struct Student s1; 只是初始化实例
// 2-2: struct Student s2 = { ... }; 初始化实例的同时赋值
// 2-3: 顺便创建结构体变量
int main() {
// struct 关键字可以省略
Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 98;
cout << "name: " << s1.name << " age: " << s1.age << " score: " << s1.score << endl;
struct Student s2 = { "小明", 19, 99 };
cout << "name: " << s2.name << " age: " << s2.age << " score: " << s2.score << endl;
s3.name = "李四";
s3.age = 20;
s3.score = 97;
cout << "name: " << s3.name << " age: " << s3.age << " score: " << s3.score << endl;
return 0;
}
33、结构体指针
#include<iostream>
using namespace std;
// 结构体指针
struct Student
{
string name;
int age;
int score;
};
int main() {
// 创建学生的结构体变量
struct Student s1 = { "张三", 18, 99 };
// 通过指针指向结构体变量
struct Student* p = &s1;
// cout << "原始的s1" << s1 << endl; 会报错
// 通过指针访问结构体变量内部
cout << "name: " << p->name << endl;
return 0;
}
34、结构体嵌套结构体
#include<iostream>
using namespace std;
struct Student
{
string name;
int age;
int score;
};
struct Teacher
{
int id;
string name;
int age;
Student student;
};
int main() {
// 结构体嵌套结构体
Teacher t;
t.name = "王老师";
t.age = 30;
t.id = 5;
t.student.name = "小王";
t.student.age = 20;
t.student.score = 99;
cout << "student name: " << t.student.name << endl;
return 0;
}
35、结构体作为函数参数
#include<iostream>
using namespace std;
struct Student
{
string name;
int age;
int score;
};
// 值传递
void printStudent(Student suudent) {
cout << "name: " << suudent.name << endl;
}
// 地址传递
void printStudent1(Student* p) {
cout << "name: " << p->name << endl;
p->score = 88;
}
int main() {
// 结构体作为函数的参数
Student student = { "张三", 19, 98 };
printStudent(student);
// 地址传递
printStudent1(&student);
cout << "修改后的score: " << student.score << endl;
return 0;
}
36、结构体中const使用场景
#include<iostream>
using namespace std;
// const 的使用场景
struct Student
{
string name;
int age;
int score;
};
// 实参中的student会拷贝一份到函数中, 因此指向的地址是不同的, 同时开辟新的空间, 因此可以修改为指针,可以减少内存空间消耗
// 要想不修改结构体中的值, 可以加一个const修饰
void printStudent(const Student* student) {
cout << "name: " << student->name << " age:" << student->age << " score:" << student->score << endl;
}
int main() {
Student student = { "张三", 19, 98 };
// 通过函数打印结构体的信息
printStudent(&student);
return 0;
}
37、结构体案例1
#include<iostream>
#include<ctime>
using namespace std;
struct Student
{
string name;
int score;
};
struct Teacher
{
string name;
Student sArray[5];
};
void alocateScore(Teacher teacher[], int len) {
// 指定一个随机数种子, time(NULL) 表示当前的值
srand((unsigned int)time(NULL));
string nameSeed = "ABCDE";
for (int i = 0; i < len; i++)
{
teacher[i].name = "Teacher_";
teacher[i].name += nameSeed[i];
for (int j = 0; j < 5; j++)
{
teacher[i].sArray[j].name = "Student_";
teacher[i].sArray[j].name += nameSeed[j];
// 创建一个随机数
int score = rand() % 60 + 40;
teacher[i].sArray[j].score = score;
}
}
}
void printInfo(Teacher teacher[], int len) {
for (int i = 0; i < len; i++)
{
cout << "teacher name:" << teacher[i].name << endl;
for (int j = 0; j < 5; j++)
{
cout << "student name: " << teacher[i].sArray[j].name << endl;
cout << "student score: " << teacher[i].sArray[j].score << endl;
}
}
}
int main() {
// 创建三名老师的数组
Teacher teacher[3];
// 给学生信息赋值
int len = sizeof(teacher) / sizeof(teacher[0]);
alocateScore(teacher, len);
// 打印
printInfo(teacher, len);
return 0;
}
38、结构体案例2
#include<iostream>
using namespace std;
// 使用函数时,函数中的所有的实参都是浅拷贝
// 对于实参数组来说,只是新拷贝了一个区域来存放数组,但是数组内部的指针指向是相同的
struct Hero
{
string name;
int age;
};
void bubbleSort(Hero hero[], int len) {
for (int i = 0; i < len; i++)
{
for (int j = 0; j < i - 1; j++)
{
if (hero[j].age > hero[i + 1].age)
{
Hero temp = hero[j];
hero[j] = hero[j + 1];
hero[j + 1] = temp;
}
}
}
}
void printHeroName(Hero hero[]) {
for (int i = 0; i < 3; i++)
{
cout << "hero name:" << hero[i].name << " age: " << hero[i].age << endl;
}
}
int main() {
Hero hero[3] = {
{"张飞", 18},
{"刘备", 20},
{"关羽", 17},
};
cout << "排序前:" << endl;
printHeroName(hero);
cout << "排序后:" << endl;
bubbleSort(hero, 3);
printHeroName(hero);
return 0;
}