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;
}