C++基础回顾

看的黑马的视频,做一下记录 

一、基础储备 

1.1 注释

作用:在代码中加一些说明和解释,方便自己或其他程序员程序员阅读代码

两种格式

  1. 单行注释// 描述信息
    • 通常放在一行代码的上方,或者一条语句的末尾,对该行代码说明
  2. 多行注释: /* 描述信息 */
    • 通常放在一段代码的上方,对该段代码做整体说明

作用:用于记录程序中不可更改的数据

1.2 常量

C++定义常量两种方式

  1. #define 宏常量: #define 常量名 常量值

    • 通常在文件上方定义,表示一个常量
  2. const修饰的变量 const 数据类型 常量名 = 常量值

    • 通常在变量定义前加关键字const,修饰该变量为常量,不可修改

C++关键字

asmdoifreturntypedef
autodoubleinlineshorttypeid
booldynamic_castintsignedtypename
breakelselongsizeofunion
caseenummutablestaticunsigned
catchexplicitnamespacestatic_castusing
charexportnewstructvirtual
classexternoperatorswitchvoid
constfalseprivatetemplatevolatile
const_castfloatprotectedthiswchar_t
continueforpublicthrowwhile
defaultfriendregistertrue
deletegotoreinterpret_casttry

1.3 sizeof关键字

数据类型占用空间取值范围
short(短整型)2字节(-2^15 ~ 2^15-1)
int(整型)4字节(-2^31 ~ 2^31-1)
long(长整形)Windows为4字节,Linux为4字节(32位),8字节(64位)(-2^31 ~ 2^31-1)
long long(长长整形)8字节(-2^63 ~ 2^63-1)

作用:利用sizeof关键字可以统计数据类型所占内存大小

语法: sizeof( 数据类型 / 变量)

1.4 转义字符

作用:用于表示一些不能显示出来的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数字0000
\ddd8进制转义字符,d范围0~73位8进制
\xhh16进制转义字符,h范围09,af,A~F3位16进制

1.5 do...while

注意:与while的区别在于do…while会先执行一次循环语句,再判断循环条件 

1.6 数组

int main() {

	//数组名用途
	//1、可以获取整个数组占用内存空间大小
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };

	cout << "整个数组所占内存空间为: " << sizeof(arr) << endl;  // 40
	cout << "每个元素所占内存空间为: " << sizeof(arr[0]) << endl;  // 4
	cout << "数组的元素个数为: " << sizeof(arr) / sizeof(arr[0]) << endl;  // 10

	//2、可以通过数组名获取到数组首地址
	cout << "数组首地址为: " << (int)arr << endl;  //数组首地址为: 496104856

	cout << "数组中第一个元素地址为: " << (int)&arr[0] << endl;  //	数组中第一个元素地址为: 496104856

	cout << "数组中第二个元素地址为: " << (int)&arr[1] << endl;  //  	数组中第二个元素地址为: 496104860

	//arr = 100; 错误,数组名是常量,因此不可以赋值


	system("pause");

	

二、函数

2.1 函数的分文件编写

作用:让代码结构更加清晰

函数分文件编写一般有4个步骤

  1. 创建后缀名为.h的头文件
  2. 创建后缀名为.cpp的源文件
  3. 在头文件中写函数的声明
  4. 在源文件中写函数的定义
//swap.h文件
#include<iostream>
using namespace std;
 
//实现两个数字交换的函数声明
void swap(int a, int b);
 
//swap.cpp文件
#include "swap.h"
 
void swap(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
 
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}
//main函数文件
#include "swap.h"
int main() {
 
	int a = 100;
	int b = 200;
	swap(a, b);
 
	system("pause");
 
	return 0;
}
 

三、指针

3.1 指针定义

指针变量定义语法: 数据类型 * 变量名;

int main() {

	int a = 10;
	int* p;
	p = &a; //指针指向数据a的地址
	cout << *p << endl; //* 解引用  10
	cout << sizeof(p) << endl; //8
	cout << sizeof(char*) << endl;  //8
	cout << sizeof(float*) << endl;  //8
	cout << sizeof(double*) << endl;  //8

	system("pause");
	return 0;
}

总结:所有指针类型在32位操作系统下是4个字节 

3.2 空指针 野指针 

空指针:指针变量指向内存中编号为0的空间                

用途:初始化指针变量

注意:空指针指向的内存是不可以访问的

int main() {

	//指针变量p指向内存地址编号为0的空间
	int* p = NULL;

	//访问空指针报错 
	//内存编号0 ~255为系统占用内存,不允许用户访问
	cout << *p << endl;

	system("pause");
	return 0;
}

野指针:指针变量指向非法的内存空间 

int main() {

	//指针变量p指向内存地址编号为0x1100的空间
	int* p = (int*)0x1100;

	//访问野指针报错 
	cout << *p << endl;

	system("pause");
	return 0;
}

3.3 const修饰指针

const修饰指针有三种情况

  1. const修饰指针 — 常量指针
  2. const修饰常量 — 指针常量
  3. const既修饰指针,又修饰常量
int main() {

	int a = 10;
	int b = 10;

	//const修饰的是指针,指针指向可以改,指针指向的值不可以更改
	const int* p1 = &a;
	p1 = &b; //正确
	//*p1 = 100;  报错


	//const修饰的是常量,指针指向不可以改,指针指向的值可以更改
	int* const p2 = &a;
	//p2 = &b; //错误
	*p2 = 100; //正确

	//const既修饰指针又修饰常量
	const int* const p3 = &a;
	//p3 = &b; //错误
	//*p3 = 100; //错误

	system("pause");
	return 0;
}

 技巧:看const右侧紧跟着的是指针还是常量, 是指针就是常量指针,是常量就是指针常量

3.4 指针、数组、函数

案例描述:封装一个函数,利用冒泡排序,实现对整型数组的升序排序

例如数组:int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };

//冒泡排序函数
void bubbleSort(int * arr, int len)  //int * arr 也可以写为int arr[]
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - 1 - i; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
 
//打印数组函数
void printArray(int arr[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << arr[i] << endl;
	}
}
 
int main() {
 
	int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
	int len = sizeof(arr) / sizeof(int);
 
	bubbleSort(arr, len);
 
	printArray(arr, len);
 
	system("pause");
 
	return 0;
}

四、结构体 

4.1 结构体基本概念

结构体属于用户自定义的数据类型,允许用户存储不同的数据类型

4.2 结构体定义和使用

语法:struct 结构体名 { 结构体成员列表 };

通过结构体创建变量的方式有三种:

  • struct 结构体名 变量名
  • struct 结构体名 变量名 = { 成员1值 , 成员2值…}
  • 定义结构体时顺便创建变量
//结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
}stu3; //结构体变量创建方式3 
 
 
int main() {
 
	//结构体变量创建方式1
	struct student stu1; //struct 关键字可以省略
 
	stu1.name = "张三";
	stu1.age = 18;
	stu1.score = 100;
	cout << "姓名:" << stu1.name << " 年龄:" << stu1.age  << " 分数:" << stu1.score << endl;
 
	//结构体变量创建方式2
	struct student stu2 = { "李四",19,60 };
	cout << "姓名:" << stu2.name << " 年龄:" << stu2.age  << " 分数:" << stu2.score << endl;
 
 
	stu3.name = "王五";
	stu3.age = 18;
	stu3.score = 80;
	cout << "姓名:" << stu3.name << " 年龄:" << stu3.age  << " 分数:" << stu3.score << endl;
 
	system("pause");
	return 0;
}

总结1:定义结构体时的关键字是struct,不可省略

总结2:创建结构体变量时,关键字struct可以省略

总结3:结构体变量利用操作符 ‘’.’’ 访问成员

4.3 结构体数组

作用:将自定义的结构体放入到数组中方便维护

语法:struct 结构体名 数组名[元素个数] = { {} , {} , ... {} }

//结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
}
 
int main() {
	
	//结构体数组
	struct student arr[3]=
	{
		{"张三",18,80 },
		{"李四",19,60 },
		{"王五",20,70 }
	};
 
	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 分数:" << arr[i].score << endl;
	}
 
	system("pause");
	return 0;
}

4.4 结构体指针

作用:通过指针访问结构体中的成员

  • 利用操作符 ->可以通过结构体指针访问结构体属性
//结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};
 
 
int main() {
	
	struct student stu = { "张三",18,100, };
	
	struct student * p = &stu;
	
	p->score = 80; //指针通过 -> 操作符可以访问成员
 
	cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
	
	system("pause");
 
	return 0;
}

 总结:结构体指针可以通过 -> 操作符 来访问结构体中的成员

4.5 结构体嵌套结构体

作用: 结构体中的成员可以是另一个结构体

例如:每个老师辅导一个学员,一个老师的结构体中,记录一个学生的结构体

//学生结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};
 
//教师结构体定义
struct teacher
{
    //成员列表
	int id; //职工编号
	string name;  //教师姓名
	int age;   //教师年龄
	struct student stu; //子结构体 学生
};
 
 
int main() {
 
	struct teacher t1;
	t1.id = 10000;
	t1.name = "老王";
	t1.age = 40;
 
	t1.stu.name = "张三";
	t1.stu.age = 18;
	t1.stu.score = 100;
 
	cout << "教师 职工编号: " << t1.id << " 姓名: " << t1.name << " 年龄: " << t1.age << endl;
	
	cout << "辅导学员 姓名: " << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分数: " << t1.stu.score << endl;
 
	system("pause");
 
	return 0;
}

4.6 结构体做函数参数

作用:将结构体作为参数向函数中传递

传递方式有两种:

  • 值传递
  • 地址传递
//学生结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};
 
//值传递
void printStudent(student stu )
{
	stu.age = 28;
	cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age  << " 分数:" << stu.score << endl;
}
 
//地址传递
void printStudent2(student *stu)
{
	stu->age = 28;
	cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age  << " 分数:" << stu->score << endl;
}
 
int main() {
 
	student stu = { "张三",18,100};
	//值传递
	printStudent(stu);
	cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
 
	cout << endl;
 
	//地址传递
	printStudent2(&stu);
	cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age  << " 分数:" << stu.score << endl;
 
	system("pause");
 
	return 0;
}

子函数中 姓名:张三 年龄: 28 分数:100
主函数中 姓名:张三 年龄: 18 分数:100

子函数中 姓名:张三 年龄: 28 分数:100
主函数中 姓名:张三 年龄: 28 分数:100

4.7 结构体中 const使用场景

作用:用const来防止误操作

//学生结构体定义
struct student
{
	//成员列表
	string name;  //姓名
	int age;      //年龄
	int score;    //分数
};
 
//const使用场景
void printStudent(const student *stu) //加const防止函数体中的误操作
{
	//stu->age = 100; //操作失败,因为加了const修饰
	cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;
 
}
 
int main() {
 
	student stu = { "张三",18,100 };
 
	printStudent(&stu);
 
	system("pause");
 
	return 0;
}

姓名:张三 年龄:18 分数:100 

五、零散 

C++ 存储类

存储类定义 C++ 程序中变量/函数的范围(可见性)和生命周期。这些说明符放置在它们所修饰的类型之前。下面列出 C++ 程序中可用的存储类:

  • auto
  • register
  • static
  • extern
  • mutable
  • thread_local (C++11)

auto 存储类

auto 关键字用于两种情况:声明变量时根据初始化表达式自动推断该变量的类型、声明函数时函数返回值的占位符。

auto f=3.14;      //double
auto s("hello");  //const char*
auto z = new auto(9); // int*
auto x1 = 5, x2 = 5.0, x3='r';//错误,必须是初始化为同一类型

register 存储类

register 存储类用于定义存储在寄存

器中而不是 RAM 中的局部变量。这意味着变量的最大尺寸等于寄存器的大小(通常是一个词),且不能对它应用一元的 '&' 运算符(因为它没有内存位置)。

{
   register int  miles;
}

寄存器只用于需要快速访问的变量,比如计数器。还应注意的是,定义 'register' 并不意味着变量将被存储在寄存器中,它意味着变量可能存储在寄存器中,这取决于硬件和实现的限制。

static 存储类

static 存储类指示编译器在程序的生命周期内保持局部变量的存在,而不需要在每次它进入和离开作用域时进行创建和销毁。因此,使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。

static 修饰符也可以应用于全局变量。当 static 修饰全局变量时,会使变量的作用域限制在声明它的文件内。

在 C++ 中,当 static 用在类数据成员上时,会导致仅有一个该成员的副本被类的所有对象共享。

#include <iostream>
 
// 函数声明 
void func(void);
 
static int count = 10; /* 全局变量 */
 
int main()
{
    while(count--)
    {
       func();
    }
    return 0;
}
// 函数定义
void func( void )
{
    static int i = 5; // 局部静态变量
    i++;
    std::cout << "变量 i 为 " << i ;
    std::cout << " , 变量 count 为 " << count << std::endl;
}

extern 存储类

extern 存储类用于提供一个全局变量的引用,全局变量对所有的程序文件都是可见的。当您使用 'extern' 时,对于无法初始化的变量,会把变量名指向一个之前定义过的存储位置。

当您有多个文件且定义了一个可以在其他文件中使用的全局变量或函数时,可以在其他文件中使用 extern 来得到已定义的变量或函数的引用。可以这么理解,extern 是用来在另一个文件中声明一个全局变量或函数。

extern 修饰符通常用于当有两个或多个文件共享相同的全局变量或函数的时候,如下所示:

第一个文件:main.cpp

#include <iostream>
 
int count ;
extern void write_extern();
 
int main()
{
   count = 5;
   write_extern();
}

 第二个文件:

#include <iostream>
 
extern int count;
 
void write_extern(void)
{
   std::cout << "Count is " << count << std::endl;
}

 实用

1.随机数生成

srand((unsigned int)time(NULL));

1.控制输入格式 

setw() 函数用于设置字段的宽度,语法格式如下:

setw(n)

n 表示宽度,用数字表示。

setw() 函数只对紧接着的输出产生作用当后面紧跟着的输出字段长度小于 n 的时候,在该字段前面用空格补齐,当输出字段长度大于 n 时,全部整体输出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值