Skip to content

⚙️ 基础

注释

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

c++
// 单行注释

/* 多行注释 */

提示:编译器在编译代码时,会忽略注释的内容

变量

作用:给一段指定的内存空间起名,方便操作这段内存

语法数据类型 变量名 = 初始值;

c++
#include<iostream>
using namespace std;

int main() {

	//变量的定义
	//语法:数据类型  变量名 = 初始值

	int a = 10;

	cout << "a = " << a << endl;

	system("pause");

	return 0;
}

注意:C++在创建变量时,必须给变量一个初始值,否则会报错

常量

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

C++定义常量两种方式

  1. #define 宏常量: #define 常量名 常量值`
  • 通常在文件上方定义,表示一个常量
  1. const修饰的变量 const 数据类型 常量名 = 常量值
  • 通常在变量定义前加关键字const,修饰该变量为常量,不可修改
c++
//1、宏常量
#define day 7

int main() {

	cout << "一周里总共有 " << day << " 天" << endl;
	//day = 8;  //报错,宏常量不可以修改

	//2、const修饰变量
	const int month = 12;
	cout << "一年里总共有 " << month << " 个月份" << endl;
	//month = 24; //报错,常量是不可以修改的

	system("pause");

	return 0;
}

标识符命名规则

  • 标识符不能是关键字
  • 标识符只能由字母、数字、下划线组成
  • 第一个字符必须为字母或下划线
  • 标识符中字母区分大小写

sizeof关键字

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

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

c++
int main() {

	cout << "short 类型所占内存空间为: " << sizeof(short) << endl;

	cout << "int 类型所占内存空间为: " << sizeof(int) << endl;

	cout << "long 类型所占内存空间为: " << sizeof(long) << endl;

	cout << "long long 类型所占内存空间为: " << sizeof(long long) << endl;

	system("pause");

	return 0;
}

整型结论:short < int <= long <= long long

浮点型

作用:用于表示小数

浮点型变量分为两种:

  1. 单精度float
  2. 双精度double

两者的区别在于表示的有效数字范围不同。

c++
int main() {

	float f1 = 3.14f;
	double d1 = 3.14;

	cout << f1 << endl;
	cout << d1<< endl;

	cout << "float  sizeof = " << sizeof(f1) << endl;
	cout << "double sizeof = " << sizeof(d1) << endl;

	//科学计数法
	float f2 = 3e2; // 3 * 10 ^ 2
	cout << "f2 = " << f2 << endl;

	float f3 = 3e-2;  // 3 * 0.1 ^ 2
	cout << "f3 = " << f3 << endl;

	system("pause");

	return 0;
}

字符串型

作用:用于表示一串字符

两种风格

  1. C风格字符串char 变量名[] = "字符串值"示例:
c++
int main() {

	char str1[] = "hello world";
	cout << str1 << endl;

	system("pause");

	return 0;
}

注意:C风格的字符串要用双引号括起来

  1. C++风格字符串string 变量名 = "字符串值"示例:
c++
int main() {

	string str = "hello world";
	cout << str << endl;

	system("pause");

	return 0;
}

注意:C++风格字符串,需要加入头文件==#include==

布尔类型

**作用:**布尔数据类型代表真或假的值

bool类型只有两个值:

  • true --- 真(本质是1)
  • false --- 假(本质是0)
c++
int main() {

	bool flag = true;
	cout << flag << endl; // 1

	flag = false;
	cout << flag << endl; // 0

	cout << "size of bool = " << sizeof(bool) << endl; //1

	system("pause");

	return 0;
}

数据的输入

作用:用于从键盘获取数据

**关键字:**cin

语法: cin &gt;&gt; 变量

c++
int main(){

	//整型输入
	int a = 0;
	cout << "请输入整型变量:" << endl;
	cin >> a;
	cout << a << endl;

	//浮点型输入
	double d = 0;
	cout << "请输入浮点型变量:" << endl;
	cin >> d;
	cout << d << endl;

	//字符型输入
	char ch = 0;
	cout << "请输入字符型变量:" << endl;
	cin >> ch;
	cout << ch << endl;

	//字符串型输入
	string str;
	cout << "请输入字符串型变量:" << endl;
	cin >> str;
	cout << str << endl;

	//布尔类型输入
	bool flag = true;
	cout << "请输入布尔型变量:" << endl;
	cin >> flag;
	cout << flag << endl;
	system("pause");
	return EXIT_SUCCESS;
}

if语句

c++
int main() {

	//选择结构-单行if语句
	//输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印

	int score = 0;
	cout << "请输入一个分数:" << endl;
	cin >> score;

	cout << "您输入的分数为: " << score << endl;

	//if语句
	//注意事项,在if判断语句后面,不要加分号
	if (score > 600)
	{
		cout << "我考上了一本大学!!!" << endl;
	}

	system("pause");

	return 0;
}
c++
int main() {

	int score = 0;

	cout << "请输入考试分数:" << endl;

	cin >> score;

	if (score > 600)
	{
		cout << "我考上了一本大学" << endl;
	}
	else
	{
		cout << "我未考上一本大学" << endl;
	}

	system("pause");

	return 0;
}
c++
	int main() {

	int score = 0;

	cout << "请输入考试分数:" << endl;

	cin >> score;

	if (score > 600)
	{
		cout << "我考上了一本大学" << endl;
	}
	else if (score > 500)
	{
		cout << "我考上了二本大学" << endl;
	}
	else if (score > 400)
	{
		cout << "我考上了三本大学" << endl;
	}
	else
	{
		cout << "我未考上本科" << endl;
	}

	system("pause");

	return 0;
}

switch语句

c++
int main() {

	//请给电影评分
	//10 ~ 9   经典
	// 8 ~ 7   非常好
	// 6 ~ 5   一般
	// 5分以下 烂片

	int score = 0;
	cout << "请给电影打分" << endl;
	cin >> score;

	switch (score)
	{
	case 10:
	case 9:
		cout << "经典" << endl;
		break;
	case 8:
		cout << "非常好" << endl;
		break;
	case 7:
	case 6:
		cout << "一般" << endl;
		break;
	default:
		cout << "烂片" << endl;
		break;
	}

	system("pause");

	return 0;
}

while循环语句

c++
int main() {

	int num = 0;
	while (num < 10)
	{
		cout << "num = " << num << endl;
		num++;
	}

	system("pause");

	return 0;
}

do...while

c++
int main() {

	int num = 0;

	do
	{
		cout << num << endl;
		num++;

	} while (num < 10);

	system("pause");

	return 0;
}

for

c++
int main() {

	for (int i = 0; i < 10; i++)
	{
		cout << i << endl;
	}

	system("pause");

	return 0;
}

break

c++
int main() {
	//在嵌套循环语句中使用break,退出内层循环
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (j == 5)
			{
				break;
			}
			cout << "*" << " ";
		}
		cout << endl;
	}

	system("pause");

	return 0;
}

continue

c++
int main() {

	for (int i = 0; i < 100; i++)
	{
		if (i % 2 == 0)
		{
			continue;
		}
		cout << i << endl;
	}

	system("pause");

	return 0;
}

goto

c++
int main() {

	cout << "1" << endl;

	goto FLAG;

	cout << "2" << endl;
	cout << "3" << endl;
	cout << "4" << endl;

	FLAG:

	cout << "5" << endl;

	system("pause");

	return 0;
}

一维数组

c++
int main() {

	//定义方式1
	//数据类型 数组名[元素个数];
	int score[10];

	//利用下标赋值
	score[0] = 100;
	score[1] = 99;
	score[2] = 85;

	//利用下标输出
	cout << score[0] << endl;
	cout << score[1] << endl;
	cout << score[2] << endl;

	//第二种定义方式
	//数据类型 数组名[元素个数] =  {值1,值2 ,值3 ...};
	//如果{}内不足10个数据,剩余数据用0补全
	int score2[10] = { 100, 90,80,70,60,50,40,30,20,10 };

	//逐个输出
	//cout << score2[0] << endl;
	//cout << score2[1] << endl;

	//一个一个输出太麻烦,因此可以利用循环进行输出
	for (int i = 0; i < 10; i++)
	{
		cout << score2[i] << endl;
	}

	//定义方式3
	//数据类型 数组名[] =  {值1,值2 ,值3 ...};
	int score3[] = { 100,90,80,70,60,50,40,30,20,10 };

	for (int i = 0; i < 10; i++)
	{
		cout << score3[i] << endl;
	}

    //数组名用途
    int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };

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

	system("pause");

	return 0;
}

函数

c++
//函数定义
int add(int num1, int num2) //定义中的num1,num2称为形式参数,简称形参
{
	int sum = num1 + num2;
	return sum;
}

int main() {

	int a = 10;
	int b = 10;
	//调用add函数
	int sum = add(a, b);//调用时的a,b称为实际参数,简称实参
	cout << "sum = " << sum << endl;

	a = 100;
	b = 100;

	sum = add(a, b);
	cout << "sum = " << sum << endl;

	system("pause");

	return 0;
}

值传递

c++
void swap(int num1, int num2)
{
	cout << "交换前:" << endl;
	cout << "num1 = " << num1 << endl;
	cout << "num2 = " << num2 << endl;

	int temp = num1;
	num1 = num2;
	num2 = temp;

	cout << "交换后:" << endl;
	cout << "num1 = " << num1 << endl;
	cout << "num2 = " << num2 << endl;

	//return ; 当函数声明时候,不需要返回值,可以不写return
}

int main() {

	int a = 10;
	int b = 20;

	swap(a, b);

	cout << "mian中的 a = " << a << endl;
	cout << "mian中的 b = " << b << endl;

	system("pause");

	return 0;
}

函数的声明

告诉编译器函数名称及如何调用函数。函数的实际主体可以单独定义。

  • 函数的声明可以多次,但是函数的定义只能有一次
c++
//声明可以多次,定义只能一次
//声明
int max(int a, int b);
int max(int a, int b);
//定义
int max(int a, int b)
{
	return a > b ? a : b;
}

int main() {

	int a = 100;
	int b = 200;

	cout << max(a, b) << endl;

	system("pause");

	return 0;
}

函数的分文件编写

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

  1. 创建后缀名为.h的头文件
  2. 创建后缀名为.cpp的源文件
  3. 在头文件中写函数的声明
  4. 在源文件中写函数的定义
c++
//swap.h文件
#include<iostream>
using namespace std;

//实现两个数字交换的函数声明
void swap(int a, int b);
c++
//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;
}
c++
//main函数文件
#include "swap.h"
int main() {

	int a = 100;
	int b = 200;
	swap(a, b);

	system("pause");

	return 0;
}

指针变量的定义和使用

c++
int main() {

	//1、指针的定义
	int a = 10; //定义整型变量a

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

	//指针变量赋值
	p = &a; //指针指向变量a的地址
	cout << &a << endl; //打印数据a的地址
	cout << p << endl;  //打印指针变量p

	//2、指针的使用
	//通过*操作指针变量指向的内存
	cout << "*p = " << *p << endl;

	system("pause");

	return 0;
}

指针变量和普通变量的区别

  • 普通变量存放的是数据,指针变量存放的是地址
  • 指针变量可以通过" * "操作符,操作指针变量指向的内存空间,这个过程称为解引用

总结1: 我们可以通过 & 符号 获取变量的地址

总结2:利用指针可以记录地址

总结3:对指针变量解引用,可以操作指针指向的内存

指针所占内存空间

c++
int main() {

	int a = 10;

	int * p;
	p = &a; //指针指向数据a的地址

	cout << *p << endl; //* 解引用
	cout << sizeof(p) << endl;
	cout << sizeof(char *) << endl;
	cout << sizeof(float *) << endl;
	cout << sizeof(double *) << endl;

	system("pause");

	return 0;
}

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

空指针和野指针

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

**用途:**初始化指针变量

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

示例1:空指针

c++
int main() {

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

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

	system("pause");

	return 0;
}

示例2:野指针

c++
int main() {

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

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

	system("pause");

	return 0;
}

总结:空指针和野指针都不是我们申请的空间,因此不要访问。

const修饰指针

const修饰指针有三种情况

  1. const修饰指针 --- 常量指针
  2. const修饰常量 --- 指针常量
  3. const即修饰指针,又修饰常量

示例:

c++
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右侧紧跟着的是指针还是常量, 是指针就是常量指针,是常量就是指针常量

指针和数组

**作用:**利用指针访问数组中元素

示例:

c++
int main() {

	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };

	int * p = arr;  //指向数组的指针

	cout << "第一个元素: " << arr[0] << endl;
	cout << "指针访问第一个元素: " << *p << endl;

	for (int i = 0; i < 10; i++)
	{
		//利用指针遍历数组
		cout << *p << endl;
		p++;
	}

	system("pause");

	return 0;
}

指针和函数

**作用:**利用指针作函数参数,可以修改实参的值

示例:

c++
//值传递
void swap1(int a ,int b)
{
	int temp = a;
	a = b;
	b = temp;
}
//地址传递
void swap2(int * p1, int *p2)
{
	int temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}

int main() {

	int a = 10;
	int b = 20;
	swap1(a, b); // 值传递不会改变实参

	swap2(&a, &b); //地址传递会改变实参

	cout << "a = " << a << endl;

	cout << "b = " << b << endl;

	system("pause");

	return 0;
}

总结:如果不想修改实参,就用值传递,如果想修改实参,就用地址传递

结构体定义和使用

语法:struct 结构体名 &#123; 结构体成员列表 &#125;;

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

  • struct 结构体名 变量名
  • struct 结构体名 变量名 = { 成员1值 , 成员2值...}
  • 定义结构体时顺便创建变量

示例:

c++
//结构体定义
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:结构体变量利用操作符 ''.'' 访问成员

结构体指针

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

  • 利用操作符 &gt;可以通过结构体指针访问结构体属性

示例:

c++
//结构体定义
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;
}

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

选择创建新项目-->空项目

函数

如果不体现生命,main方法 在函数的前面就会报错

有了函数的声明,main方法,可以写在函数的前面

函数的民生,可以写很多次,但是实现(定义)只能写一次

c++
#include <iostream>
using namespace std;

// 提前告诉编译器
// 函数的民生,可以写很多次,但是实现(定义)只能写一次
int max(int num1, int num2);
int max(int num1, int num2);
int max(int num1, int num2);

int main()
{
	 int sum = max(1, 2);

	cout << "计算器的结果:" << sum << endl;

	system("pause");
	return 0;
}

int max(int num1, int num2) {
	int sum = num1 + num2;

	return sum;
}

常量指针

特点:指针的指向可以改,指针指向的值不可以改

c++
const int * p = &a;
*0 = 20  错误
p = &b   对的

指针常量

特点:指针的指向不可以改,指针指向的值可以改

c++
int * const p = &a;
*0 = 20  对的
p = &b   错误

const 同时修饰指针和常量

c++
const int * const p = &a;
*0 = 20  错的
p = &b   错误

例子:

c++
#include <iostream>
using namespace std;

int main()
{

	int a = 10;
	int b = 20;

	int* p = &a;
	cout << "第一个测试指针a地址:"<<p << "===" << *p  << endl;
	int* p111 = &b;
	cout << "第一个测试指针b地址:"<< p111 << "===" << *p111  << endl;

	// const 修饰指针
	const int* p1 = &a;
	// *p=20 错误
	cout << "第二个测试指针地址:" << p1 << "===" << *p1 << endl;
	p1 = &b;
	cout << "第二个测试指针地址:" << p1 << "===" << *p1 << endl;

	// const 修饰常量
	int* const p2 = &a;
	cout << "第三个测试指针地址:" << p2 << "===" << *p2 << endl;
	*p2=30;
	// p = &b; 错误
	cout << "第三个测试指针地址:" << p2 << "===" << *p2 << endl;

	// const 修饰指针和常量
	const int* const p3 = &a;

	system("pause");
	return 0;
}

技术笔记