引言
记录从零开始学习C++的过程。
记录
2024-09-06
无符号和有符号数字
整型
有符号则表示可以为负数,无符号则从0开始。
signed int num1 = 30;//有符号整型
int num2 = -30;//有符号整形,signed可省略
unsigned int num3 = 30;//无符号整形,必须大于0
//无符号变量快捷写法
u_int num4 = 1;
u_short num5 = 2;
u_long num6 = 3;
实型
float,double,long double
实型默认有符号,即无signed或unsigned
常量可以具有后缀,如0LL
U : 表示无符号整形(unsigned)
L : 表示long类型或long double 类型
LL : 表示long long 类型
UL:表示unsigned long 类型
ULL: 表示 unsigned long long 类型
F :表示float 类型
注:整数类型默认int,小数类型默认double
char类型
char类型本质上是数字,即在内存中实际上存储的是数字
通过ASCII码表作为对照,字符 -> 数字存入,数字 -> 字符使用

转义字符
常用的
\n 换行符
\t 制表符,用于对齐
\' 打印'
\" 打印"
\ 打印\
2024-09-07
字符串
//C语言风格字符串
char str1[] = "你好"; //字符数组形式字符串
char *str2 = "你好"; //指针形式字符串
//C++语言风格字符串
string str3 = "你好";
字符串拼接
使用+号拼接,如果存在int型变量,需要用to_string()函数转化为字符串
数据输入 cin
int a; //声明a
cin >> a; //输入传给a
string name;
cout << "请输入姓名:" << endl;
cin >> name;
int age;
cout << "请输入年龄:" << endl;
cin >> age;
算术运算符
+,-,*,/,%(取余),++,--
a = 2;
b = ++a;//先运算后赋值
// a = 3 , b = 3
a = 2;
b = a++;//先赋值后运算
// a = 3 , b = 2
比较运算符
c语言风格字符串在用比较运算符比较时,比较的是内存地址,因此需要用strcmp(s1,s2)函数进行比较
#include "cstring"
int result = strcmp(s1,s2);//-1 s1<s2 ,0 s1=s2,1 s1>s2
while循环
案例1:输出1-100的和
int i = 1;
int sum = 0;
while (i <= 100) {
sum += i;
i++;
}
cout << "1-100的和为:" << sum << endl;
案例2:猜一个100以内的数字
#include "iostream"
#include "random"
using namespace std;
int getRandom(int x, int y) {
mt19937 engine(std::random_device{}());
uniform_int_distribution<int> distribution(x, y);
int randomNumber = distribution(engine);
return randomNumber;
}
int main() {
int randomnum = getRandom(1,100);
int input_num;
int guess_times = 0;
cout << "请输入一个1到100的数字" << endl;
cin >> input_num;
while (!(randomnum == input_num)) {
if (randomnum > input_num) {
cout << "小了" << endl;
}
else {
cout << "大了" << endl;
}
cout << "请重新输入" << endl;
cin >> input_num;
guess_times++;
}
cout << "猜中了!你一共猜了"<< guess_times << "次" << endl;
return 0;
}
案例3:九九乘法表
// 九九乘法表
int row = 1;
while (row<=9) {
int col = 1;
while (col <= row) {
cout << col << "×" << row << "=" << row * col << "\t";
col++;
}
cout << endl;
row++;
}
do while循环
案例1:猜一个100以内的数字
#include "iostream"
#include "random"
using namespace std;
int getRandom(int x, int y) {
mt19937 engine(std::random_device{}());
uniform_int_distribution<int> distribution(x, y);
int randomNumber = distribution(engine);
return randomNumber;
}
int main() {
int randomnum = getRandom(1,100);
cout << randomnum << endl;
int input_num;
int guess_times = 0;
cout << "请输入一个1到100的数字" << endl;
do {
guess_times++;
cin >> input_num;
if (randomnum > input_num) {
cout << "小了" << endl;
}
else if (randomnum < input_num){
cout << "大了" << endl;
}
else {
cout << "猜中了" << endl;
break;
}
cout << "请重新输入" << endl;
} while (randomnum != input_num);
cout << "你一共猜了"<< guess_times << "次" << endl;
return 0;
}
for循环
案例1:发工资
int balance = 10000;
for (int i = 1; (i <= 20) && (balance>0); i++) {
int rank = getRandom(1,10);
if (rank <5) {
cout << "员工" << i << ",绩效分" << rank << ",低于5,不发工资,下一位" << endl;
}
else {
balance -= 1000;
cout << "向员工" << i << "发放工资1000元,账面余额还剩" << balance << "元" << endl;
}
}
2024-09-09
数组遍历for循环
//普通写法
int arr[] = {1,2,3,4,5};
for(int i;i<sizeof(arr)/sizeof(arr[0]);i++){
//code
}
//高级写法
int arr[] = {1,2,3,4,5};
for(int i: arr){
//code
}
2024-09-10
数组遍历
#include "iostream"
using namespace std;
int main() {
int v[2][2] = {
{1,2},{1,2}
};
for (int i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
for (int j = 0; j < sizeof(v[0]) / sizeof(v[0][0]); j++) {
cout << v[i][j] << endl;
}
}
return 0;
}
指针运算
指针+n或-n,地址加 *类型n**
//int
int num = 1;
int* p = #
cout << p << endl;//000000C5EE8FFC24
p++;
cout << p << endl;//000000C5EE8FFC28
//double
double num = 1;
double* p = #
cout << p << endl;//000000B361AFF908
p++;
cout << p << endl;//000000B361AFF910
用途
对数组进行操作
int v[] = { 1,2,3 };
int *vp = v;
cout << *vp << endl;//1
vp++;
cout << *vp << endl;//2
vp++;
cout << *vp << endl;//3
//或
int v[] = { 1,2,3 };
int *vp = v;
cout << *(vp) << endl;
cout << *(vp + 1) << endl;
cout << *(vp + 2) << endl;
因为v实际上记录的是v数组中0号元素的地址
动态内存分配
概念:由C++自动分配的内存,称之为(自动)静态内存分配,不会进行内存空间的自动清理。(无垃圾回收机制)只有程序结束或所在作用域结束才会清理。于是我们需要手动管理内存,用完清理。
这里用到了new和delete运算符
//new type
//new type[]
//delete 指针
//delete[] 指针
int *p = new int;
*p = 10;
cout << *p << endl;
delete p;
int* p = new int[5];
p[0] = 1; // 等同于*(p+0)
*p = 1;
p[1] = 3; // 等同于*(p+1)
cout << p[0] << endl;
cout << *(p + 1) << endl;
delete[] p;
用完就删,也就是说在new和delete中写代码。
2024-09-11
数组元素移除
案例:从数组中取出偶数形成新数组
int* pArr = new int[10] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int* newarr = new int[5];
for (int i = 0; i < 10; i++) {
if ((pArr[i] % 2) == 0) {
newarr[(i - 1) / 2] = pArr[i];
}
}
delete[] pArr;
for (int j = 0; j < 5; j++) {
cout << "新数组中的元素为:" << newarr[j] << endl;
}
delete[] newarr;
数组元素添加
案例:向数组中添加元素形成新数组
int* pArr = new int[3] {1, 3, 5};
int* newarr = new int[5];
int offset = 0;
for (int i = 0; i < 5; i++) {
if (i == 1) {
offset++;
newarr[i] = 2;
continue;
}
else if (i == 3) {
offset++;
newarr[i] = 4;
continue;
}
newarr[i] = pArr[i - offset];
}
delete[] pArr;
for (int j = 0; j < 5; j++) {
cout << "新数组中的元素为:" << newarr[j] << endl;
}
2024-09-12
常量指针
int num = 10,num2 = 20;
const int * p = #//指向const的指针,p的指向可以更改,p指向的内容不可修改
int * const p = &num2;//指向不可变,数据可变
const int * const p = &num1;//指向不可变,数据不可变
应用场景:需要常量的同时也需要动态内存分配的场景
2024-09-13
对数组升序处理
//类似冒泡排序
int* pArr = new int[10] {5, 2, 6, 34, 12, 78, 3, 5, 12, 7};
int* a = new int;
int offset = 9;
int count = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < offset; j++) {
count++;
if (pArr[j] > pArr[j + 1]) {
*a = pArr[j + 1];
pArr[j + 1] = pArr[j];
pArr[j] = *a;
}
}
offset--;
}
cout << "执行了" << count << "次" << endl;//45次
delete a;
for (int i = 0; i < 10; i++) {
cout << "升序后数组:" << pArr[i] << endl;
}
delete[] pArr;
结构体
案例:录入五个学生的姓名,年龄,地址
struct Student {
string name;
int age;
string address;
};
struct Student* stu = new Student[5];
for (int i = 0; i < 5; i++) {
cout << "请输入姓名:";
cin >> stu[i].name;
cout << endl;
cout << "请输入年龄:";
cin >> stu[i].age;
cout << endl;
cout << "请输入地址:";
cin >> stu[i].address;
cout << endl;
}
for (int i = 0; i < 5; i++) {
cout <<"姓名:"<< stu[i].name << endl;
cout <<"年龄:"<< stu[i].age << endl;
cout <<"地址:"<< stu[i].address << endl;
}
delete[] stu;
for (int i = 0; i < 5; i++) {
cout << "姓名:" << stu[i].name << endl;
cout << "年龄:" << stu[i].age << endl;
cout << "地址:" << stu[i].address << endl;
}
函数
案例1:获取三个数中最小数
int get_min(int a,int b,int c) {
int min;
min = a;
if (a > b) {
min = b;
if (b > c) {
min = c;
}
}
else if (a < b) {
min = a;
if (a > c) {
min = c;
}
}
else if(a==b){
if (a > c) {
min = c;
}
}
return min;
}
案例2:找最大最小,返回结构体(函数嵌套调用)
int get_min(int a, int b) {
int min;
min = a;
if (a > b) {
min = b;
}
return min;
}
int get_max(int a, int b) {
int max;
max = b;
if (a > b) {
max = a;
}
return max;
}
struct MinAndMax {
int min;
int max;
};
struct MinAndMax get_struct(int a, int b) {
int min, max;
int min = get_min(a, b);
int max = get_max(a, b);
struct MinAndMax srt = {min,max};
return srt;
}
int main() {
int num1, num2;
struct MinAndMax srt = {};
cout << "请输入第一个数" << endl;
cin >> num1;
cout << "请输入第二个数" << endl;
cin >> num2;
srt = get_struct(num1, num2);
cout << "结构体中最小值为" << srt.min << endl;
cout << "结构体中最大值为" << srt.max << endl;
return 0;
}
案例3:银行余额查询,存取款
#include "iostream"
using namespace std;
void query_balance(int * inital_balance,string *name) {
cout << *name << ",您好,您的余额剩余" << *inital_balance << "元" << endl;
}
void save_money(int* inital_balance, string *name,int* money) {
*inital_balance += *money;
cout << name << ",您好,您存款" << *money << "元成功" << endl;
query_balance(inital_balance, name);
}
void get_money(int* inital_balance, string *name, int* money) {
*inital_balance -= *money;
cout << name << ",您好,您取款" << *money << "元成功" << endl;
query_balance(inital_balance, name);
}
int main() {
int* inital_balance = new int(5000000);
string *name = new string;
int choose;
int* money = new int;
cout << "请输入姓名:" << endl;
cin >> *name;
while (true) {
cout << "----------主菜单----------" << endl;
cout << *name << ",您好,欢迎来到银行ATM。请选择操作:" << endl;
cout << "查询余额[输入1]" << endl;
cout << "存款\t[输入2]" << endl;
cout << "取款\t[输入3]" << endl;
cout << "退出\t[输入0]" << endl;
cin >> choose;
switch (choose) {
case 1:
cout << "----------查询余额----------" << endl;
query_balance(inital_balance,name);
break;
case 2:
cout << "----------存款----------" << endl;
cout << *name << ",您好,您要存多少元:";
cin >> *money;
save_money(inital_balance, name, money);
break;
case 3:
cout << "----------取款----------" << endl;
cout << *name << ",您好,您要取多少元:";
cin >> *money;
get_money(inital_balance, name, money);
break;
}
if (choose == 0) {
return 0;
}
}
return 0;
}
2024-09-21
引用的本质
//自动转换为int* const ref = &a;
int& ref = a;
2025-03-25
类和对象
struct和class
struct的默认成员权限是public
class的默认成员权限是private