目录
输入语法:cin >> 变量;
输出语法:cout << 输出内容 << endl;
暂停功能:system("pause");
清屏功能:system("cls");
注意:
- #include
- using namespace std;
- void main()
- {
- int a = 0;
- //endl为换行
- cout << "请输入a的值:" << endl;
- cin >> a;
- cout << "a的值为:" << a << endl;
- system("cls");
- system("pause");
- }
- #include
- #include
- using namespace std;
- void main()
- {
- //添加随机数种子(使用当前不断变化的时间生成随机数,防止每次随机数都一样)
- //time()对应库函数#include
- srand((unsigned int)time(NULL));
- //rand()代表任意的随机数
- int a = rand();
- //代表100以内的随机数
- int num = a % 100 + 1;
- //对应using namespace std;与#include
- cout << num << endl;
- //主要起到一个暂停功能(对应库函数#include
) - system("pause");
- }
- #include
- using namespace std;
- void main() {
- while (true) {
- system("pause");
- //退出程序
- return;
- }
- }
- #include
- using namespace std;
- void main() {
- while (true) {
- system("pause");
- //退出程序
- exit(0);
- }
- }
注意:exit()括号内传的参数相当于程序退出后要返回的代码,传的参数为整形(传入浮点数会返回舍去浮点数的整数);
含义:C++关键字是C++中预先保留的单词

注意:在定义变量或者常量的时候不要使用关键字,否则会产生歧义
含义:C++规定给标识符命名时,有一套自己的规则
注意:在给标识符命名时,争取做到见名知意的效果,方便自己和他人的阅读
前言:C++规定在创建一个变量或者常量时,必须要指定出相应的数据类型,否则无法给变量分配内存
作用:整型变量表示的是整数类型的数据
注意:C++中能够表示整数类型有以下几种方式,区别在于所占内存空间不同

作用:用于表示小数

- #include
- using namespace std;
- int main()
- {
- float f = 3.1415926f;
- float f1 = 233e-2;
- cout << "f的值为:" << f << endl;
- cout << "f1的值为:" << f1 << endl;
- double d = 3.1415926;
- cout << "d的值为:" << d << endl;
- //最终结果都为3.14159
- system("pause");
- return 0;
- }
注意:
作用:字符型变量用于显示单个字符
语法:char ch= 'a';
注意:
- #include
- using namespace std;
- int main()
- {
- int a = 98;
- int a1 = 'b';
- char b = 98;
- char b1 = 'b';
- cout << "a的值为:" << a << endl;//98
- cout << "a1的值为:" << a1 << endl;//98
- cout << "b的值为:" << b << endl;//b
- cout << "b1的值为:" << b1 << endl;//b
- system("pause");
- return 0;
- }
注意:本质上字符的储存就是将字符对应的ASCII码存到内存中,若以int类型接收得到的就是int类型数据,若以char类型接收,得到的就是char类型数据
作用:用于表示一串字符
- #include
- using namespace std;
- void main() {
- string str1 = "hello world";
- //成功,string类型可以直接为整个数组赋值
- str1 = "你好,世界";
- //打印:你好世界
- cout << str1 << endl;
- char str2[] = "hi C++";
- //失败,char数组不可以直接为数组赋值
- str2 = "你好,C++";
- //报错
- cout << str2 << endl;
- }
结果:
作用:布尔数据类型代表真或假的值
语法:bool flag=true;
注意:bool数据类型占1byte的大小
- #include
- using namespace std;
- void main()
- {
- bool flag = true;
- cout << "true值为:" << flag << endl;//1
- flag = false;
- cout << "false值为:" << flag << endl;//0
- system("pause");
- }
关键字:typeid(数据类型/数据).name()
- #include
- using namespace std;
- void main() {
- cout <<"int类型的数据类型为:"<<typeid(int).name() << endl;
- cout <<"hello字符串的数据类型为:"<<typeid("hello").name() << endl;
- }
含义:用来组织和重用代码的一个内存单元
关键字:namespace
语法:
- namespace 命名空间名{
- 代码区
- }
注意:
1.使用作用域符号::进行访问
- //创建命名空间
- namespace name_1 {
- int num = 1;
- void sayHello() {
- cout << "hello world!" << endl;
- }
- }
- void main() {
- //使用作用域符号进行访问
- name_1::sayHello();
- }
2.通过using关键字指定命名空间后直接访问该命名空间内容
- using namespace name_1;
- //创建命名空间
- namespace name_1 {
- int num = 1;
- void sayHello() {
- cout << "hello world!" << endl;
- }
- }
- void main() {
- //通过using关键字来使用命名空间的内容
- sayHello();
- }
3.通过using关键字指定命名空间的具体内容后访问
- using name_1::sayHello;
- //创建命名空间
- namespace name_1 {
- int num = 1;
- void sayHello() {
- cout << "hello world!" << endl;
- }
- }
- //主函数
- void main() {
- sayHello();
- }
语法:namespace 新名=旧名;
- //创建命名空间
- namespace name_1 {
- int num = 1;
- void sayHello() {
- cout << "hello world!" << endl;
- }
- }
- //给命名空间起别名
- namespace A = name_1;
- //主函数
- void main() {
- //使用别名访问命名空间的内容
- A::sayHello();
- }
前言:C++中利用new操作符程序员可以在堆区手动开辟数据空间;程序员也可以手动释放空间,释放空间利用操作符delete
开辟空间语法:new 数据类型[(构造参数)]
返回值:该数据对应类型的指针
释放空间语法:delete 要释放的地址;
开辟数组语法:new 数据类型[数组长度]
返回值:对应开辟出数组空间的首地址(指针)
释放数组空间语法:delete[] 要释放的地址;
- #include
- using namespace std;
- int* func() {
- //在堆区创建空间
- int* p = new int(10);
- return p;
- }
- void testArr() {
- //在堆中创建长度为10的整形数组
- int* arr = new int[10];
- for (int i = 0; i < 10; i++) {
- //给10个元素赋值
- arr[i] = i + 100;
- }
- for (int i = 0; i < 10; i++) {
- cout << arr[i] << endl;
- }
- //释放数组空间地址
- delete[] arr;
- }
- void main() {
- testArr();
- int* p = func();
- cout << "a的值为:" << *p << endl;
- cout << "a的值为:" << *p << endl;
- //堆内释放地址
- delete p;
- //内存已经被释放,再次访问就是非法操作
- cout << "a的值为:" << *p << endl;
- }
作用:给变量起别名
语法:数据类型 &别名=原名
- #include
- using namespace std;
- void main() {
- //引用的基本语法
- int a = 10;
- int &b = a;
- b = 20;
- cout << "a的值为:" << a << endl;//20
- cout << "b的值为:" << b << endl;//20
- system("pause");
- }
理解:变量名本身就是自己所表示的值的一块内存空间,值的地址表示变量,因此,值与别名在某种意义上是等价的,所以引用必须要初始化,并且初始化后便不可更改
作用:函数传参时可以利用引用的技术让形参修饰实参
- #include
- using namespace std;
- void swap(int &a,int &b) {
- int temp = a;
- a = b;
- b = temp;
- }
- void main() {
- int a = 10;
- int b = 20;
- swap(a, b);
- cout << "a的值为:" << a << "\tb的值为:" << b << endl;
- system("pause");
- }
结果:由此观之,引用传递,那么形参的改变会影响实参
- #include
- using namespace std;
- //以引用的形式返回a
- int& change() {
- //全局区的变量a
- static int a = 10;
- return a;
- }
- void main() {
- int &ref = change();
- //10
- cout << "ref的值为:" << ref << endl;
- cout << "ref的值为:" << ref << endl;
- system("pause");
- }
- #include
- using namespace std;
- //以引用的形式返回a
- int& change() {
- //全局区的变量a
- static int a = 10;
- return a;
- }
- void main() {
- int &ref = change();
- //相当于将a的引用赋值为1000
- change() = 1000;
- //因为ref为a的引用所以也为1000
- cout << "ref的值为:" << ref << endl;
- system("pause");
- }
结论:若函数返回值类型为引用类型,则其函数调用可以作为左值
本质:在C++内部实现是一个指针常量

结论:C++推荐使用引用技术,因为语法方便,引用的本质是指针常量,但是所有的指针操作编译器都帮助我们做了
作用:常量引用主要用来修饰形参,防止误操作
- #include
- using namespace std;
- void main() {
- //加上const后,编译器将代码改为:int temp=10; int& ref=temp;
- const int& ref = 10;
- //下面报错,加了const后引用的值不可更改
- //ref = 20;
- cout << "ref的值为:" << ref << endl;
- system("pause");
- }
注意:加了const后,那么被引用的别名值不可被更改
- #include
- using namespace std;
- void showValue(const int& a) {
- //这里形参到的改变会影响实参,因此加const来防止误操作
- cout << "a的值为:" << a << endl;
- }
- void main() {
- int a = 100;
- showValue(a);
- system("pause");
- }
前言:在C++中函数的形参列表中的形参是可以有默认值的
- 语法:
- 返回值类型 函数名(参数=默认值){
- 函数体;
- }
- #include
- using namespace std;
- int add(int a, int b=20, int c=30) {
- return a+b+c;
- }
- void main() {
- int sum=add(10);
- cout << "sum等于:" << sum << endl;//60
- system("pause");
- }
C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
- 语法:
- 返回值类型 函数名(数据类型){
- 函数体;
- }
注意:占位参数也可以有默认值
- #include
- using namespace std;
- //占位参数
- void func(int a,int) {
- cout << "占位参数实例!" << endl;
- }
- void main() {
- //占位参数函数的调用
- func(10, 20);
- system("pause");
- }
- #include
- using namespace std;
- //占位参数的默认参数
- void func(int a,int =10) {
- cout << "占位参数实例!" << endl;
- }
- void main() {
- //占位参数函数的调用
- func(10);
- system("pause");
- }
含义:函数名相同,但参数列表不同,进而提高函数的复用性
注意:函数的返回值不可以作为函数重载的条件的
- #include
- using namespace std;
- void func() {
- cout << "func的调用:" << endl;
- }
- void func(int a) {
- }
- void main() {
- func();
- func(10);
- system("pause");
- }
函数重载的注意事项
引用作为重载的条件
- #include
- using namespace std;
- void func(int &a) {
- cout << "func(int &a)的调用:" << endl;
- }
- void func(const int &a) {
- }
- void main() {
- int a = 10;
- //调用第一个
- func(a);
- //调用第二个
- func(10);
- system("pause");
- }
函数重载碰到函数默认参数
- #include
- using namespace std;
- void func(int a,int b=10) {
- cout << "func(int a,int b=10)的调用:" << endl;
- }
- void func(int a) {
- cout << "func(int a)的调用:"<< endl;
- }
- void main() {
- int a = 10;
- //若传一个参数的话会出现二义性
- func(10,20);
- system("pause");
- }
注意:当函数重载碰到默认参数时,会出现二义性,报错,应尽量避免这种情况。
内联函数
前言
含义:通过内存膨胀的方式,以空间换取时间的函数
本质:通过inline关键字将对应函数的代码放到代码区,进而避免了函数调用时在栈区临时开辟内存,进而减少了函数调用过程入栈和出栈的时间
目的:提高程序的运行速度
语法:
- inline 返回值类型 方法名(参数列表){
- 函数体;
- }
具体案例
- #include
- using namespace std;
- inline void func(int num);
- void main() {
- func(9);
- }
- inline void func(int num) {
- cout << "调用了内联函数,num值为:" << num << endl;
- }
C++之异常处理机制
异常:在程序运行过程中发生了不正常的现象,阻止了程序的运行。
异常处理关键字
- throw:抛出异常
- try:测试一些有异常的代码
- catch:对上面所抛出的异常进行捕获并解决,进而代码可以继续向后运行
异常语法
- try{
- 可能出现异常的代码;
- }catch(接收异常){
- 处理异常;
- }catch(接收异常){
- 处理异常;
- }catch(...){
- 处理异常;
- }
具体案例
- #include
- using namespace std;
- double test(double a, double b) {
- //对异常情况进行处理
- if (b == 0) {
- throw "被除数不能为0";
- //throw后,后面代码不执行
- }
- return a / b;
- }
- //主函数
- void main() {
- try {
- cout <<"a/b的值:" << test(9, 0) << endl;
- }
- catch (const char* e) {
- cout << e << endl;
- }
- catch (...) {
- cout << "其他错误!" << endl;
- }
- }
注意:
- throw抛出的异常信息支持多种类型,抛出的异常信息被catch内的内容接收
- throw抛出异常后,若没有catch进行捕获,那么后面的代码不执行
- 对于抛出的其他异常,catch后面可以用...来捕获
自定义异常处理
前言
C++中提供了一些供我们使用的异常类

注意:使用上面的异常时需要加头文件#include 同时也需要使用std命名空间
具体案例
- #include
- #include
- using namespace std;
- class MyException :public exception {
- public:
- //重写what函数
- const char* what()const {
- return "自定义的异常";
- }
- };
- void main() {
- try {
- //直接抛异常测试
- throw MyException();
- }
- catch (MyException& obj) {
- cout << obj.what() << endl;
- }
- }
C++的文件操作
前言:
背景:程序运行时产生的数据都为临时数据,程序一旦运行结束都会被释放,通过文件可以将数据持久化
注意:C++中对文件操作需要包含头文件#include
文件类型分为两种
- 文本文件:文件以文本的ASCII码的形式存储在计算机中
- 二进制文件:文件以文本的二进制形式储存在计算机中,用户一般不能直接读懂他们
注意:二进制不单单可以操作内置数据类型,同时也可以操作自定义数据类型
操作文件的三大类
- ofstream:写操作
- ifstream:读操作
- fstream:读写操作
以文本的方式写文件
写文件操作步骤
- 包含头文件:#include
- 创建流对象:ofstream ofs;
- 打开文件:ofs.open("文件路径",打开方式);
- 写数据:ofs<<"写入的数据";
- 关闭文件:ofs.close();
文件的打开方式

注意:文件打开方式可以配合使用,利用|操作符
例如:用二进制方式写文件:ios::binary|ios::out
具体案例
- #include <iostream>
- //包含文件的头文件
- #include <fstream>
- using namespace std;
- //文本文件的写文件
- void writeText() {
- //创建流对象
- ofstream ofs;
- //指定文件的打开方式
- ofs.open("C:\\All\\other\\test.txt", ios::out);
- //向文件中写内容
- ofs << "hello C++" << endl;
- //关闭文件
- ofs.close();
- }
- void main() {
- writeText();
- }
注意:
- 文件路径可以使用相对路径以及绝对路径
- 写文件可以使用ofstream流对象或fstream流对象
- 文件写完了close关闭文件
以文本的方式读文件
读文件操作步骤
- 包含头文件:#include
- 创建流对象:ifstream ifs;
- 打开文件并判断文件是否打开成功:ifs.open("文件路径",打开方式);
- 读数据:四种方式进行读取
- 关闭文件:ifs.close();
具体案例
- #include <iostream>
- //包含文件的头文件
- #include <fstream>
- using namespace std;
- //文本文件的读文件
- void readText() {
- //创建流对象
- ifstream ifs;
- //打开文件并判断文件是否打开成功
- ifs.open("C:\\All\\other\\test.txt", ios::in);
- if (!ifs.is_open()) {
- cout << "文件打开失败" << endl;
- return;
- }
- //读取文件数据
- char buf[1024] = { 0 };
- while (ifs>>buf) {
- cout << buf << endl;
- }
- //关闭文件
- ifs.close();
- }
- void main() {
- readText();
- }
注意:
- 判断文件是否打开成功:ifs.is_open(),若返回真则打开成功,反之打开失败
- 写文件可以使用ofstream流对象或fstream流对象
- 文件读完了close关闭文件
读文件的四种方法
方法1
- void readText() {
- //创建流对象
- ifstream ifs;
- //打开文件并判断文件是否打开成功
- ifs.open("C:\\All\\other\\test.txt", ios::in);
- if (!ifs.is_open()) {
- cout << "文件打开失败" << endl;
- return;
- }
- //读取文件数据
- char buf[1024] = { 0 };
- while (ifs>>buf) {
- cout << buf << endl;
- }
- //关闭文件
- ifs.close();
- }
注意:将文件内容写入buf数组,文件若读到末尾则会返回假的标志:ifs>>buf (将文件内容读进buf)
方法2
- void readText() {
- //创建流对象
- ifstream ifs;
- //打开文件并判断文件是否打开成功
- ifs.open("C:\\All\\other\\test.txt", ios::in);
- if (!ifs.is_open()) {
- cout << "文件打开失败" << endl;
- return;
- }
- //读取文件数据
- char buf[1024] = { 0 };
- while (ifs.getline(buf, sizeof(buf))) {
- cout << buf << endl;
- }
- //关闭文件
- ifs.close();
- }
注意:ifs.getline(字符串首地址,读取的长度);为读取一行的意思,若读取到的有值就会返回真,反之返回假(字符串首地址表示将文件内容读到哪个地方)
方法3
使用前需要导入头文件:#include
- void readText() {
- //创建流对象
- ifstream ifs;
- //打开文件并判断文件是否打开成功
- ifs.open("C:\\All\\other\\test.txt", ios::in);
- if (!ifs.is_open()) {
- cout << "文件打开失败" << endl;
- return;
- }
- //读取文件数据
- string buf;
- while (getline(ifs, buf)) {
- cout << buf << endl;
- }
- //关闭文件
- ifs.close();
- }
注意:getline(基础输入流, 准备好的字符串),该函数为全局函数,若基础输入流能读到数据则返回真,反之返回假
方法4
- void readText() {
- //创建流对象
- ifstream ifs;
- //打开文件并判断文件是否打开成功
- ifs.open("C:\\All\\other\\test.txt", ios::in);
- if (!ifs.is_open()) {
- cout << "文件打开失败" << endl;
- return;
- }
- //读取文件数据
- char c;
- while ((c=ifs.get())!= -1){
- cout << c;
- }
- //关闭文件
- ifs.close();
- }
注意:该方法为从文件一个字符一个字符的读,若读到文件末尾EOF,则会返回假进而结束循环
以二进制的方式对文件进行写操作
二进制方式写文件主要利用流对象调用成员函数write
函数原型:ostream& write(const char* buffer,int len)
函数参数:字符指针buffer指向内存中的一段储存空间表示从哪开始写入文件,len是读写的字节数
具体案例
- #include <iostream>
- //包含文件的头文件
- #include <fstream>
- using namespace std;
- //二进制文件的写文件
- class Person {
- public:
- char m_Name[64];
- int m_Age;
- };
- void writeBin() {
- //创建流对象
- ofstream ofs;
- //打开文件
- ofs.open("C:\\All\\other\\bin.txt",ios::out|ios::binary);
- //写入文件内容
- Person p = { "张三",18 };
- ofs.write((const char*)&p, sizeof(Person));
- //关闭文件
- ofs.close();
- }
- void main() {
- writeBin();
- }
二进制方式的读文件
二进制的读文件主要利用流对象调用成员函数read
函数原型:istream& read(char *buffer,int len);
参数解释:字符指针buffer指向内存中的一段储存空间表示读到哪个位置,len是读写的字节数表示读多少
- #include <iostream>
- //包含文件的头文件
- #include <fstream>
- using namespace std;
- //二进制文件的读文件
- class Person {
- public:
- char m_Name[64];
- int m_Age;
- };
- void readBin() {
- //创建流对象
- ifstream ifs;
- //打开文件
- ifs.open("C:\\All\\other\\bin.txt", ios::in | ios::binary);
- //判断文件是否打开成功
- if (!ifs.is_open()) {
- cout << "文件打开失败!" << endl;
- return;
- }
- //读文件读到Person中
- Person p;
- ifs.read((char*)&p, sizeof(Person));
- cout << "姓名:" << p.m_Name << "\n年龄:" << p.m_Age << endl;
- //关闭文件
- ifs.close();
- }
- void main() {
- readBin();
- }
注意:读自定义数据类型的对象时自定义的类型必须存在
-
相关阅读:
C高级 Linux中的文件相关指令
【C++】set/multiset/map/multimap
工厂WMS系统货架位管理:优化仓储效率
安全协议的匿名性
@vitejs/plugin-legacy 为你的 Vite 项目提供对旧版浏览器的支持
【推荐一款阿里开源的低代码工具,实用性极高!】
C++结构体定义 & 创建 & 赋值 & 结构体数组
12、Kubernetes中KubeProxy实现之iptables和ipvs
内联函数的探究
TS(TypeScript)和axios实现VUE请求重试
-
原文地址:https://blog.csdn.net/m0_60027772/article/details/128071670