C++学习笔记(Ⅰ):C++基础入门

视频链接:黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili

目录

1 C++初识

1.1 第一个C++程序

 1.2 注释

1.3 变量

1.4 常量

1.5 关键字

1.6 标识符命名规则

2 数据类型

2.1 整型

2.2 sizeof

2.3 实型(浮点型)

2.4 字符型

2.5 转义字符

2.6 字符串 

2.7 布尔类型

2.8 数据输入

3 运算符

3.1 算术运算符

3.2 赋值运算符

3.3 比较运算符

3.4 逻辑运算符

4 程序流程结构

4.1 选择结构

1.if语句

2.三目运算符

3.switch语句

4.2 循环语句

1.whle

2.练习

3.do while

4.练习

5.for循环

6.练习

7.嵌套循环

8.练习

4.3 跳转语句

5 数组

5.1 概述

5.2 一维数组

1.定义方式

2.数组名称

3.练习

4.冒泡排序

5.3 二维数组

1.定义方式

2.数组名称

3.练习

6 函数

6.1 函数定义

6.2 函数调用

6.3 值传递

6.4 常见样式

6.5 函数声明

6.6 函数的分文件编写

7 指针

7.1 指针定义

7.2 空指针和野指针

1.空指针

2.野指针

7.3 const修饰指针

1.const修饰指针--常量指针

2.const修饰常量--指针常量

3.const

7.4 指针和数组

7.5 指针和函数

7.6 练习

8 结构体

8.1 结构体定义

8.2 结构体数组

8.3 结构体指针

8.4 结构体嵌套

8.5 结构体做函数参数

8.6 const

8.7 练习


1 C++初识

1.1 第一个C++程序

 C++可使用的IDE包括经典的DEV C++、VS Code、VS、Clion等,这里因前期Python编译主要使用VS Code进行,且VS所需内存实在过大,因此仍选择VS Code作为IDE。输出第一个代码“Hello World”

#include <iostream>
using namespace std;
int main()
{
	cout << "hello world" << endl;
	
	system("pause");

	return 0;

}

 1.2 注释

// 单行注释

/* `````` */ 多行注释

main是程序入口,在源文件中必须存在且仅有一个。程序只运行main函数

1.3 变量

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

#include <iostream>
using namespace std;

int main()
{

	int a = 10;

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

	system("pause");

	return 0;
}

1.4 常量

#include <iostream>
using namespace std;
// #define 宏常量定义,不可修改
// const 修改变量为常量

#define Day 7

int main()
{
	// 1.宏常量
	cout << "一周总共有:" << Day << "天" << endl;

	// 2.const
	const int month = 12;
	cout << "一年总共有:" << month << "月" << endl;

	system("pause");

	return 0;
}

1.5 关键字

C++中不得使用关键字作为变量名

1.6 标识符命名规则

1.标识符不能是关键字;

2.只能由数字、字母、下划线组成

3.首位必须是下划线或字母

4.标识符中区分字母大小写

2 数据类型

2.1 整型

short短整型(2byte)、int整型(4byte)、long长整型(Windows 4byte/Linux32位4byte/Linux64位8byte)、long long长长整型(8byte)

#include<iostream>
using namespace std;

int main()
{
	short num1 = 10;

	int num2 = 10;

	long num3 = 10;

	long long num4 = 10;

	cout << "num1 = " << num1 << endl;
	cout << "num2 = " << num2 << endl;
	cout << "num3 = " << num3 << endl;
	cout << "num4 = " << num4 << endl;

	system("pause");
	return 0;
}

2.2 sizeof

统计数据类型所占内存大小

#include<iostream>
using namespace std;

int main()
{
	// sizeof(数据类型/变量)

	short num1 = 10;

	int num2 = 10;

	long num3 = 10;

	long long num4 = 10;

	cout << "short内存: " << sizeof(num1) << endl;
	cout << "int内存: " << sizeof(int) << endl;
	cout << "long内存: " << sizeof(num3) << endl;
	cout << "long long内存: " << sizeof(num4) << endl;

	system("pause");
	return 0;
}

2.3 实型(浮点型)

1.单精度float(4byte,7位有效数字)

2.双精度double(8byte,15-16位有效数字)

#include<iostream>
using namespace std;

int main()
{
	// 默认显示六位有效数字
	float f1 = 3.14f;
	double f2 = 3.14;

	cout << "f1: " << f1 << endl;
	cout << "f2: " << f2 << endl;

	// 科学计数法

	float f3 = 3e2;

	cout << "f3: " << f3 << endl;

	system("pause");
	return 0;
}

2.4 字符型

#include<iostream>
using namespace std;

int main()
{
	// 只占用1个字节,只能使用1个字符
	// 将字符对应的ASCII编码送入存储单元
	char ch = 'a'; // 务必使用单引号 

	cout << ch << endl;
	// 输出ch的ASCII编码
	cout << (int)ch << endl;

	system("pause");
	return 0;
}

2.5 转义字符

#include<iostream>
using namespace std;

int main()
{
	// 表示不能显示的ASCII字符

	// 换行
	cout << "hello world\n";

	// 反斜杠
	cout << "\\" << endl;

	// 水平制表 \t占8个位 

	cout << "aaa\thello" << endl;
	cout << "aa\thello" << endl;
	cout << "aaaa\thello" << endl;

	system("pause");

	return 0;
}

2.6 字符串 

#include<iostream>
#include<string>
using namespace std;

int main()
{
	// 1.C风格:char 变量名[] = "字符串值"
	char str[] = "hello world";
	cout << str << endl;
	// 2. C++风格:string 变量名 = "字符串值" 
	// 需包含头文件 #include<string>

	string str2 = "hello world";
	cout << str2 << endl;
	system("pause");

	return 0;
}

2.7 布尔类型

true表示真(1),Flase表示假(0),占1个字节  

2.8 数据输入

从键盘获取数据:cin

#include<iostream>
#include<string>
using namespace std;

int main()
{
	// 定义变量
	int a = 0;

	// 设置输入
	cin >> a;

	cout << a << endl;
	system("pause");

	return 0;
}

使用ctrl + k + c 注释所选择的内容

3 运算符

3.1 算术运算符

执行四则运算:+加、-减、*乘、/除

%取余、前置/后置递增/递减

#include<iostream>
using namespace std;

int main()
{
	// 四则运算,整数相除结果依旧为整数,去除小数部分

	int a = 1;
	int b = 2;

	cout << a + b << endl;  // 3
	cout << a - b << endl;  // -1
	cout << a * b << endl;  // 2
	cout << a / b << endl;  // 0

	// 取模运算,两小数不可进行取模运算

	int num1 = 10;
	int num2 = 3;

	cout << num1 % num2 << endl;  // 1

	// 递增/减运算

	// 1.前置递增,先加1再运算
	int c = 10;
	++c;
	cout << c << endl;  // 11

	// 2.后置递增,先运算再加1
	int d = 10;
	d++;
	cout << d << endl;  // 11

	system("pause");

	return 0;

}

3.2 赋值运算符

#include<iostream>
using namespace std;

int main()
{
	// +=
	int a = 1;
	a += 2;

	cout << a<< endl;  

	system("pause");

	return 0;

}

3.3 比较运算符

#include<iostream>
using namespace std;

int main()
{
	// ==相等
	// !=不相等
	// <小于 >大于 <=小于等于 >=大于等于
	int a = 1;
	int b = 2;

	cout << (a == b) << endl;   // ()里为优先运算

	system("pause");

	return 0;

}

3.4 逻辑运算符

#include<iostream>
using namespace std;

int main()
{
	// !非
	int a = 1;

	cout << !a << endl;   //0
	// && 与
	int b = 0;
	cout << (a && b) << endl;   //0
	// || 或
	int c = 0;
	cout << (a||c) << endl;   //1

	system("pause");

	return 0;

}

4 程序流程结构

4.1 选择结构

1.if语句

#include<iostream>
using namespace std;

int main()
{
	 // 顺序结构:按顺序执行程序,不跳转 

	// 选择结构:单行if语句

	int score = 0;
	cout << "Input your score:" << endl;

	cin >> score;

	if (score > 600)
	{
		cout << "ok" << endl;
	}
	// 选择结构:if-else语句
	int score = 0;
	cout << "Input your score:" << endl;

	cin >> score;
	if (score > 600)
	{
		cout << "ok" << endl;
	}

	else
	{
		cout << "No" << endl;
	}

	// if多条件语句
	int score = 0;
	cout << "Input your score:" << endl;

	cin >> score;

	if (score > 600) {
		cout << "1" << endl;
	}
	else if (score > 500) {
		cout << "2" << endl;
	}
	else {
		cout << "3" << endl;
	}
	// 嵌套if语句
	int score = 0;
	cout << "Input your score:" << endl;

	cin >> score;

	if (score > 600) {
		cout << "1" << endl;
		if (score > 700) {
			cout << "0" << endl;
		}
	} 
	system("pause");

	return 0;

}

exercise

#include<iostream>
using namespace std;
int main() {
	// 三个数比大小,比最大数
	int a = 0, b = 0, c = 0;
	cout << "Input a:" << endl;
	cin >> a;
	cout << "Input b:" << endl;
	cin >> b;
	cout << "Input c:" << endl;
	cin >> c;
	if (a > b) {
		if (a > c) {
			cout << "a MAX " << endl;
		}
		else {
			cout << "c MAX" << endl;
		}
	}
	else {
		if (b > c)
		{
			cout << "b MAX" << endl;
		}
		else 
		{
			cout << "c MAX" << endl;
		}
	}

	system("pause");
}

2.三目运算符

// 三目运算符,返回变量,可继续赋值
	int a = 10, b = 20, c = 0;

	c=(a > b ? a : b);

	cout << c << endl;

3.switch语句

	// switch语句

	int score = 0;
	cin >> score;

	switch (score)
	{
	case 10 :
		cout << "10" << endl;
		break;
	case 9:
		cout << "9" << endl;
		break;
	case 8:
		cout << "8" << endl;
		break;
	default:
		cout << "<8" << endl;
		break;
	}

4.2 循环语句

1.whle

#include<iostream>
using namespace std;
int main() 
{
	int num = 0;

	while (num < 10)
	{
		cout << num++ << endl;
	}
	system("pause");

	return 0;
}

2.练习

#include<iostream>
using namespace std;
#include<ctime>
int main() 
{
	// 随机数种子,利用系统时间生成随机数
	srand((unsigned int)time(NULL));
	// 猜测1-100间的数字
	// 1.生成随机数
	int num = rand() % 100 + 1;   // 生成0+1~99+1的随机数
	// 2.玩家猜测
	int val = 0;
	while (1)
	{
		cin >> val;
		// 3. 判断猜测
		if (val > num)
		{
			cout << "Big" << endl;
		}
		else if (val < num)
		{
			cout << "Small" << endl;
		}
		else
		{
			cout << "Right" << endl;
			break;
		}
	}
	system("pause");

	return 0;
}

3.do while

#include<iostream>
using namespace std;
#include<ctime>
int main() 
{
	// do while,先执行再判断
	int num = 0;

	do
	{
		cout << num++ << endl;
	} while (num < 10);

	system("pause");

	return 0;
}

4.练习

#include<iostream>
using namespace std;
#include<ctime>
int main() 
{
	// 水仙花数,即3位数,个位上数字的三次幂之和等于其本身
	// 找到100--999的水仙花数
	int num = 100;

	do
	{
		int a, b, c  = 0;

		a = num % 10;
		b = num / 10 % 10;
		c = num / 100;

		if (a*a*a + b*b*b + c*c*c == num)
		{
			cout << num << endl;
			
		}
		num++;
	} 
	while (num < 1000);
	// 判断水仙花数

	system("pause");

	return 0;
}

5.for循环

#include<iostream>
using namespace std;
#include<ctime>
int main() 
{
	// for循环 
	for (int i = 0; i < 10; i++)
	{
		cout << i << endl;
	}
	system("pause");

	return 0;
}

6.练习

#include<iostream>
using namespace std;
#include<ctime>
int main() 
{
	//逢7必过,1-100
	for (int i = 1;i < 101; i++)
	{
		if (i % 7 == 0 || i / 10 == 7 || i % 10 == 7)
		{
			cout << i << endl;
		}
	}

	system("pause");

	return 0;
}

7.嵌套循环

#include<iostream>
using namespace std;
#include<ctime>
int main() 
{
	// 嵌套循环,打印*图
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			cout << "* ";
		}	
		cout << endl;
	}

	system("pause");
	return 0;
}

8.练习

#include<iostream>
using namespace std;
#include<ctime>
int main() 
{
	// 乘法口诀表 
	for (int i = 1; i < 10; i++)
	{
		for (int j = 1; j < 10; j++)
		{
			if (j <= i)
			{
				cout << i*j << " \t";
			}
		}

		cout << endl;
	}
	system("pause");

	return 0;
}

4.3 跳转语句

1.break跳出循环

2.continue不执行后续循环

3.goto语句跳转至标记位置

5 数组

5.1 概述

1.数组中每个数据元素都是相同的数据类型,是由连续内存组成的

5.2 一维数组

1.定义方式

数据类型 数组名[数组长度];

数据类型 数组名[数组长度]={值};用0填补未赋值的元素

数据类型 数组名[]={值};

下标从0开始

2.数组名称

统计数组在内存中的长度,并可获取数组在内存中的首地址。

3.练习

#include<iostream>
using namespace std;
int main()
{
	// 练习1 五只小猪体重
	int arr[5] = { 300,350,200,400,250 };

	int max = 0;

	for (int i = 0; i < 5; i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
		}
	}

	cout << max << endl;
	system("pause");

	return 0;

}

#include<iostream>
using namespace std;
int main()
{
	// 练习2 数组元素逆置
	int arr[5] = { 1,3,2,5,4 };

	int start = 0;
	int end = sizeof(arr) / sizeof(arr[0]) - 1;
	
	while (start < end)
	{
		int temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
		start++;
		end--;
	}

	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << endl;
	}
	
	system("pause");

	return 0;

}

4.冒泡排序

排序总轮数 = 元素个数 - 1

每轮对比次数 = 元素个数 - 排序轮数 - 1

#include<iostream>
using namespace std;
int main()
{
	//  冒泡排序实现升序排列
	int arr[] = { 4,2,8,0,5,7,1,3,9 };

	// 输出排序前数组元素

	for (int i = 0; i < 9; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;

	// 冒泡排序
	for (int i = 0; i < 9 - 1; i++)
	{
		// 内层循环
		for (int j = 0; j < 9 - i - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{	
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
	// 排序后
	for (int i = 0; i < 9; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;

	system("pause");

	return 0;

}

5.3 二维数组

1.定义方式

数据类型 数组名称[行数][列数] = { {元素},{元素} }

2.数组名称

通过数组名称可以查看所占内存空间及二维数组首地址。 

查看具体元素的地址需使用取址符&

3.练习

#include<iostream>
using namespace std;
#include<string>
int main()
{
	// 已知三同学单科成绩,分别输出三同学总成绩

	int score[3][3] = { {100,100,100},{90,50,100},{60,70,80} };

	string name[3] = { "zhangsan","lisi","wangwu" };
	for (int i = 0; i < 3; i++)
	{
		int sum = 0;
		for (int j = 0; j < 3; j++)
		{
			sum += score[i][j];
		}
		cout <<name[i]<< sum << endl;
	}
	system("pause");

	return 0;
}

6 函数

6.1 函数定义

#include<iostream>
using namespace std;
int add(int num1, int num2) {
	
	int sum = num1 + num2;
	cout << sum << endl;

	return sum;
}

6.2 函数调用

#include<iostream>
using namespace std;
int add(int num1, int num2) {
	
	// num1.num2称为形式参数
	int sum = num1 + num2;
	cout << sum << endl;

	return sum;
}
int main() {
	// a,b称为实际参数
	int a = 1;
	int b = 50;
	add(a,b);

	// 函数调用时,实参的值会传递给形参

	system("pause");

	return 0;
}

6.3 值传递

值传递,即函数调用时将实参的值传入形参,形参改变时不会影响实参。

函数若无需返回值,数据类型选用void

6.4 常见样式

1.无参无返

2.有参无返

3.无参有返

4.有参有返

6.5 函数声明

程序按顺序执行,没用定义函数前自定义函数不能写在主函数之后。

声明可以重复,但定义只能唯一。 

6.6 函数的分文件编写

1.创建后缀名.h的头文件

2.创建.cpp的源文件

3.在头文件中写函数声明

4.在源文件中写函数定义

7 指针

通过指针可以间接访问内存

7.1 指针定义

数据类型 *  指针变量; 

#include<iostream>
using namespace std;
int main()
{
	// 1.定义指针
	int a = 10;
	int * p;
	// 2.指针记录变量地址
	p = &a;
	cout <<"a的地址:"<< &a << endl;
	cout << "p为:" << p << endl;
	// 3.使用指针:解引用操作,找到指针指向内存中的数据
	*p = 1000;
	cout << "a=" << a << endl;
	cout << "*p=" << *p << endl;

	system("pause");

	return 0;
}

32位操作系统指针所占空间为4字节 

7.2 空指针和野指针

1.空指针

指针变量指向内存中编号为0的空间,用于初始化指针变量,该内存由系统占据不可访问

int * p = NULL;

2.野指针

指针变量指向非法空间,需避免出现。

7.3 const修饰指针

1.const修饰指针--常量指针

const int * p = &a;

指针的指向可以修改,但指向的值不可修改。 

2.const修饰常量--指针常量

int * const p = &a;

指针的指向不可改,但指向的值可以改。

3.const

const int * const p = &a;

指针的指向和指向的值都不可以改。

7.4 指针和数组

#include<iostream>
using namespace std;
int main()
{
	// 创建数组,利用指针访问数组中元素
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };

	int * p = arr;

	cout << "首元素" << *p << endl;

	p++;

	cout << "第二个元素" << *p << endl;

	system("pause");

	return 0;
}

7.5 指针和函数

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

#include<iostream>
using namespace std;
// 实现两个数字交换
void swap(int *a, int *b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
	cout << a << " " << b << endl;
}
int main()
{
	int a = 10;
	int b = 20;
	// 地址传递
	swap(&a, &b);

	cout << a <<" "<< b << endl;
	system("pause");

	return 0;
}

7.6 练习

利用指针将数组传入函数中

#include<iostream>
using namespace std;

// 封装函数,利用冒泡排序,实现对整型数组的升序排序
// 冒泡排序函数
void bubbleSort(int *arr,int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}

int main()
{
	// 1.创建数组
	int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
	int len = sizeof(arr) / sizeof(arr[0]);

	// 2.创建函数
	bubbleSort(arr, len);
	// 3.打印数组
	for (int i=0; i < 10; i++)
	{
		cout << arr[i];
	}
	cout << endl;
	system("pause");

	return 0;
}

8 结构体

8.1 结构体定义

属于用户自定义的数据类型,用于存储不同的数据类型。属于一些类型的集合(类似Python的字典?)

struct 结构体名 变量名

struct 结构体名 变量名 = {...}

结构体创建时,struct关键字可以省略

#include<iostream>
using namespace std;
#include<string>
// 结构体定义
struct student
{
	string name;
	int age;
	int score;
};

int main()
{
	// 结构体创建方式1
	struct student stu1;
	stu1.name = "pink";
	stu1.age = 18;
	stu1.score = 60;

	cout << stu1.name << stu1.age << stu1.score << endl;
	// 结构体创建方式2
	struct student stu2 = { "white",20,100 };
	cout << stu2.name << stu2.age << stu2.score << endl;
	system("pause");

	return 0;
}

8.2 结构体数组

将自定义的结构体送入数组中

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

#include<iostream>
using namespace std;
#include<string>
// 结构体数组
// 1.定义结构体
struct student
{
	string name;
	int age;
	int score;
};

int main()
{
	// 2.创建结构体数组
	student stuarray[3]=
	{
		{"white",18,100},
		{"pink",20,60},
		{"yellow",16,75}
	};
	// 3.遍历结构体元素
	for (int i = 0; i < 3; i++)
	{
		cout << stuarray[i].name << stuarray[i].age << stuarray[i].score << endl;
	}
	system("pause");

	return 0;
}

8.3 结构体指针

利用->通过结构体指针访问结构体属性

#include<iostream>
using namespace std;
#include<string>

// 结构体指针
struct student 
{
	string name;
	int age;
	int score;
};

int main()
{
	// 创建学生结构体变量

	student stu = { "zhangsan",15,100 };

	// 通过指针指向结构体变量
	student *p = &stu;

	// 通过指针访问结构体变量中的数据
	cout << p->name << p->age << p->score << endl;
	system("pause");

	return 0;
}

8.4 结构体嵌套

#include<iostream>
using namespace std;
#include<string>

// 结构体嵌套
struct student
{
	string name;
	int age;
	int score;
};

struct teacher
{
	int id;
	string name;
	int age;
	struct student stu;
};
int main()
{
	teacher t = { 10000,"white",50,"pink",20,60 };

	cout << t.name << t.id << t.age << t.stu.name << t.stu.age << t.stu.score << endl;
	system("pause");

	return 0;
}

8.5 结构体做函数参数

将结构体作为参数向函数中传递,包括值传递和地址传递

#include<iostream>
using namespace std;
#include<string>

// 结构体函数传递 
struct student
{
	string name;
	int age;
	int score;
};
// 1.值传递
void printStudent(student stu)
{
	cout << stu.name << stu.age << stu.score << endl;
}

// 2.地址传递
void printStudent2(student * p)
{
	cout << p->name << p->age << p->score << endl;
}
int main()
{
	student stu = { "zhangsan",18,60 };
	printStudent(stu);
	printStudent2(&stu);
	system("pause");

	return 0;
}

8.6 const

防止误操作

#include<iostream>
using namespace std;
#include<string>

// 结构体const
struct student
{
	string name;
	int age;
	int score;
};
// 将函数中形参改为指针,可以减少内存空间
// const防止误操作
void print(const student *stu)
{
	cout << stu->name << stu->age << stu->score << endl;
}
int main()
{
	student stu = { "jack",18,60 };

	print(&stu); 

	system("pause");

	return 0;
}

8.7 练习

#include<iostream>
#include<string>
#include<ctime>
using namespace std;

// 结构体案例:3个老师各带5个学生,完成毕业设计任务
// 学生结构体 
struct student
{
	string s_name;
	int score;
};

// 老师结构体
struct teacher
{
	string t_name;
	student sarray[5];
};

// 给老师和学生赋值的函数
void allocateSpcae(teacher tarray[], int len)
{
	string nameSeed = "ABCDE";
	// 给老师赋值
	for (int i = 0; i < len; i++)
	{
		tarray[i].t_name = "teacher_";
		tarray[i].t_name += nameSeed[i];
		// 给学生赋值
		for (int j = 0; j < 5; j++)
		{
			tarray[i].sarray[j].s_name = "student_";
			tarray[i].sarray[j].s_name += nameSeed[j];
			int random = rand() % 100;
			tarray[i].sarray[j].score = random;
		}
	}
}

void printInfo(teacher tarray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "老师姓名:" << tarray[i].t_name << endl;
		for (int j = 0; j < 5; j++)
		{
			cout << "\t学生姓名:" << tarray[i].sarray[j].s_name ;
			cout << " 学生成绩:" <<tarray[i].sarray[j].score << endl;
		}
		cout << endl;
	}
}

int main()
{
	// 随机数种子
	srand((unsigned int)time(NULL));
	// 1.创建老师数组
	teacher tarray[3];

	// 2.利用函数给两个结构体赋值
	int len = sizeof(tarray) / sizeof(tarray[0]);
	allocateSpcae(tarray, len);

	// 3.打印信息
	printInfo(tarray, len);

	system("pause");

	return 0;
}
#include<iostream>
#include<string>
using namespace std;

/* 设计一个包含任务姓名、年龄、性别,共存放五人的结构体
通过冒泡排序,将数组中的人按年龄进行排序并打印 */

// 1.创建结构体
struct heros
{
	string name;
	int age;
	string gender;
};

void bubbleSort(heros array[], int len)
{
	for (int i = 0; i < len; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (array[j].age > array[j + 1].age)
			{
				heros temp = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp;
			}
		}

	}
}

int main()
{
	// 2.创建结构体数组
	heros array[5] = {
		{ "liubei",  23,"male"},
		{ "guanyu",  22,"male" },
		{ "zhangfei",20,"male" },
		{ "zhaoyun", 21,"male" },
		{ "diaochan",19,"female" },
	};
	for (int i = 0; i < 5; i++)
	{
		cout << "姓名:" <<array[i].name << " "
			<< "年龄:"  << array[i].age << " "
			<< "性别:"<< array[i].gender << " "
			<< endl;
	}
	cout << endl;

	// 3.冒泡排序
	bubbleSort(array, 5);
	for (int i = 0; i < 5; i++)
	{
		cout << "姓名:" << array[i].name << " "
			<< "年龄:" << array[i].age << " "
			<< "性别:" << array[i].gender << " "
			<< endl;
	}

	system("pause");

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值