本文主要是介绍C++ 路漫漫其修远兮,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基础语法
什么是常量:不可以修改的量
有宏常量,和const常量
#include <iostream>
//常量,不能改变的量
//有宏常量和const常量
#define Day 7
using namespace std;int main() {cout << "一周有:"<< Day << "天" << endl;//宏常量const int Month = 12; //const常量cout << "一年有:" <<Month << "月" << endl;return 0;
}
整型 不同整型所占内存空间大小不同
#include <iostream>
using namespace std;//不同整型之间的区别在与所在内存空间大小不同
int main() {//短整型 2字节 有符号的所以要占一位 2^15 -32768~~32767short Num1 = 32767;//整型 四个字节 有符号 2^31 21亿 无符号的就是2^32 42亿int Num2 = 32768;//长整型 四个字节 有符号 2^31 21亿 无符号的就是2^32 42亿long Num3 = 10;//长长整型 八个字节 有符号 2^63 无符号的就是2^64long long Num4 = 10;cout << Num1 << endl;cout << Num2 << endl;cout << Num3 << endl;cout << Num4 << endl;return 0;
}
#include <iostream>
using namespace std;//大小关系//short < int <= long <=longlong
int main() {//短整型 2字节 有符号的所以要占一位 2^15 -32768~~32767short Num1 = 32767;//整型 四个字节 有符号 2^31 21亿 无符号的就是2^32 42亿int Num2 = 32768;//长整型 四个字节 有符号 2^31 21亿 无符号的就是2^32 42亿long Num3 = 10;//长长整型 八个字节 有符号 2^63 无符号的就是2^64long long Num4 = 10;cout << Num1 << endl;cout << Num2 << endl;cout << Num3 << endl;cout << Num4 << endl;return 0;
}
实型(浮点型)及科学计数法
#include <iostream>
using namespace std;int main()
{// 1、float 浮点型单精度 4字节// 2、double 浮点型双精度 8字节// 3、都显示6个float f1 = 3.1415926f;cout << f1 << endl;double d1 = 3.1415926;cout << d1 << endl;cout << "float sizeof:" << sizeof(float) << endl;cout << "double sizeof:" << sizeof(double) << endl;// 4、科学计数法float f2 = 3e2; //e为正 则3*10^2cout << f2 << endl;double d2 = 3e-2; //e为负 则3*0.1^2cout << d2 << endl;return 0;
}
字符型变量及ASSIC编码
C 和 C++ 中字符型变量只占用 ==1 个字节 == 。
字符型变量并不是把字符本身放到内存中存储,而是将对应的 ASCII 编码放入到存储单元
#include <iostream>
using namespace std;int main()
{// 1.字符新变量创建方式char ch = 'a';cout << ch << endl;// 2.字符型变量所占内存大小cout << "字符型变量所占内存大小:" << sizeof(char) << endl;// 3.字符型变量常见错误//char ch = "a"//char ch = 'abch'//字符型变量对应ASSCII编码 A:65 a:97cout << (int)'a' << endl;cout << (int)'A' << endl;return 0;
}
ASCII 码大致由以下 两部分组 成:
ASCII 非打印控制字符: ASCII 表上的数字 0-31 分配给了控制字符,用于控制像打印机等一些外围
设备。
ASCII 打印字符:数字 32-126 分配给了能在键盘上找到的字符,当查看或打印文档时就会出现。
c++中字符串
#include <iostream>
#include <string>
using namespace std;int main()
{// 1.c风格字符串char str1[] = "Hello World!";cout << str1 << endl;// 2.c++风格字符串 c++需要加string头文件string str2 = "Hello World!";cout << str2 << endl;cin.get();return 0;
}
bool类型真假 及大小
#include <iostream>
using namespace std;//bool大小为1 1byte 1个字节8bit
int main()
{// 1.创建bool数据类型 ture=1 false=0bool flag = true;cout << flag << endl;flag = false;cout << flag << endl;// 2.查看bool类型所占内存大小cout << "bool大小:" <<sizeof(bool) << endl;cin.get();return 0;
}
输入 不同类型(cin)
#include <iostream>
#include <string>
using namespace std;int main()
{// 1.整型 ctrl+k+c注释 ctrl+k+u取消注释int a = 1;cout << "请输入a的值:" << endl;cin >> a;cout << "a的值是:" << a << endl;//2.浮点型float b = 3.14f;cout << "请输入b的值:" << endl;cin >> b;cout << "b的值是:" << b << endl;// 3.字符型double d = 3.1415;cout << "请输入d的值:" << endl;cin >> d;cout << "d的值是:" << d << endl;// 4.字符串型 cin输入的字符串不能有空格string str = "hello";cout << "请输入str的值:" << endl;cin >> str;cout << "str的值是:" << str << endl;// 5.布尔类型 true非0为真 false =0 假bool flag = false;cout << "请输入flag的值:" << endl;cin >> flag;cout << "flag的值是:" << flag << endl;cin.get();return 0;
}
取模 只有整型变量才能取模运算
#include <iostream>
#include <string>
using namespace std;int main()
{// 1.两整数取模运算 10/3=3......1int a1 = 10;int b1 = 3;cout << a1 % b1 << endl;//小的对大的整数取模 值是小的 10/20 = 0......10int a2 = 10;int b2 = 20;cout << a2 % b2 << endl;// 2.两个数相除不可以为0 int a4 = 10;int b4 = 0;cout << a4 % b4 << endl;// 3.两个小数不能取模运算//float a3 = 3.66;//float b3 = 2.24;//cout << a3 % b3 << endl;return 0;
}
++a和a++前置后置区别
#include <iostream>
#include <string>
using namespace std;int main()
{// 1.前置递增 递减 先变量++ 再计算int a1 = 10;int b1 = 20;cout << ++a1 << endl;cout << --b1 << endl;// 2.后置递增 递减 先计算 再变量++int a2 = 10;int b2 = 20;cout << a2++ << endl;cout << b2-- << endl;// 3.前置和后置的区别int a3 = 10;int b3 = ++a3 * 3;//先变量++ 再计算cout << b3 << endl;int a4 = 10;int b4 = a4++ * 3;//先计算 再变量++cout << b4 << endl;return 0;
}
选择结构多个分支 只走一路
#include <iostream>
#include <string>
using namespace std;int main()
{// 1. if单分支int score = 0;cout << "请输入成绩" << endl;cin >> score;cout << "您的成绩为:" << score << endl;if (score >= 600) {cout << "恭喜您!考上了一本大学" << endl;}else if(score > 500){cout << "恭喜您!考上了二本大学" << endl;}else if (score > 400) {cout << "恭喜您!考上了三本大学" << endl;}else {cout << "很遗憾!您没有大学读" << endl;}return 0;
}
三目运算符 可返回赋值语句
#include <iostream>
#include <string>
using namespace std;int main()
{// 1.三目运算符表达式int a = 10;int b = 20;int c;c = (a > b ? a : b);cout << "最大值:" << c << endl;cout << a << endl;cout << b << endl;// 2.三目运算符可以返回赋值语句 优先级:先括号,再从右往左c = (a < b ? a : b) = 100; cout << "最大值:" << c << endl;cout << a << endl;cout << b << endl;return 0;
}
switch和if得区别
注意 1 : switch 语句中表达式类型只能是整型或者字符型
注意 2 : case 里如果没有 break ,那么程序会一直向下执行
总结:与 if 语句比,对于多条件判断时, switch 的结构清晰,执行效率高,缺点是 switch 不可以判
断区间
#include <iostream>
#include <string>
using namespace std;int main()
{// 1. switch应用int score = 0;cout << "请输入电影分数" << endl;cin >> score;cout << "电影分数是:" << score << endl; switch (score) {case 10:case 9 :cout << "这是经典电影" << endl; break;case 8 :cout << "这是好看电影" << endl; break;case 7 :cout << "这个电影还不错" << endl; break;case 6 :cout << "这个电影一般" << endl; break;default:cout << "这是垃圾电影" << endl; break;}// 2. switch和if区别// switch 缺点,判断时只能时整型或者字符型,不可以是一个区间// switch 优点,结构清晰,执行效率高return 0;
}
while猜拳小游戏
注意:在执行循环语句时候,程序必须提供跳出循环的出口,否则出现死循环
#include <iostream>
#include <string>
#include <ctime>using namespace std;int main()
{// 生成种子时间随机数 根据实时时间srand((unsigned)time(NULL));int score = 0;int flag = 8;// 1.生成随机数// 1.1 rand 会随机生成0~32767(0x7fff 2^15次方)的常量// 1.2 %100取模 会得到0~99的随机整数 +1int num = rand() % 100 + 1;// 2.while不断输入判断 给五次机会while (flag > 0) {cout << "请输入随机数 您还有" << flag << "次机会" << endl;cin >> score;if (score > num) {cout << "输入的太大了" << endl;}else if(score < num) {cout << "输入的太小了" << endl;}else {cout << "输入正确" << num << endl;break;}flag--;}return 0;
}
水仙花数
#include <iostream>
#include <string>
#include <ctime>using namespace std;int main()
{// 1.什么是水仙花数:个位三次方+十位三次方+百位三次方 = 这个数本身// 1.1 100-999 获取个位 = num%10// 1.2 获取十位 = num/10%10// 1.3 获取百位 = num/100// 2. 100-1000的水仙花数有153、370、371、407int num = 100;do {int a = num % 10;int b = num / 10 % 10;int c = num / 100;if (a* a* a + b * b * b + c * c * c == num) {cout << num << endl;}num++;}while (num < 1000);return 0;
}
99乘法表for嵌套行小于等于列
#include <iostream>
using namespace std;
int main()
{// 1.打印99乘法表 列*行// 1.1 列数永远小于等于行数//i是行数 先一行一列、再第二行第一列 第二行第二列、再第三行第一列 第三行第二列 第三行第三列for (int i = 1; i < 10; i++) {// j是列数for (int j = 1; j <= i; j++) {cout << j << "*" << i << "=" << i * j << " ";//显示的时候列在前面}cout << endl;}return 0;
}
跳转语句 goto
#include <iostream>
using namespace std;
int main()
{// 1.goto语句应用场景cout << "1 xxxxx" << endl;goto Flag;cout << "2 xxxxx" << endl;cout << "3 xxxxx" << endl;Flag:cout << "4 xxxxx" << endl;cout << "5 xxxxx" << endl;return 0;
}
数组的三种表示方式及特点
特点 1 : 数组中的每个 == 数据元素都是相同的数据类型 ==
特点 2 : 数组是由 == 连续的内存 == 位置组成的
#include <iostream>
using namespace std;
int main()
{// 1.数组特点// 1.1 连续的存储空间// 1.2 只能存放同一个数据类型// 1.3 数组下标从0开始// 2.数组的三表表示// 2.1//int arry1[3];//arry1[0] = 1;//arry1[1] = 2;//arry1[2] = 3;//for (int i = 0; i < 3; i++) {// cout << arry1[i] << endl;//}// 2.2//int arry2[5] = { 10,20,30,40,50 };//for (int i = 0; i < 5; i++) {// cout << arry2[i] << endl;//}// 2.3int arry3[] = { 10,20,30,40,50,60,70,90 };for (int i = 0; i < sizeof(arry3)/sizeof(arry3[0]); i++) {cout << arry3[i] << endl;}return 0;
}
一维数组名的作用
一维数组名称的 用途 :
1. 可以统计整个数组在内存中的长度
2. 可以获取数组在内存中的首地址
#include <iostream>
using namespace std;
int main()
{// 数组名作用// 1.数组名可以计算数组大小,从而计算出数组长度int arr[] = { 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;// 2.数组名可以知道数组的首地址 -- 数组名即首地址:以十六进制显示cout << arr << endl;cout << &arr[0] << endl;for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {cout << &arr[i] << endl;}// 3.数组名不能赋值,是不能修改的常量// arr = 10;return 0;
}
一维数组逆置 通过小标循环移位交换数据
#include <iostream>
using namespace std;
int main()
{// 1.创建数组int arr[] = { 1,3,5,7,8,6,9 };for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {cout << arr[i];}cout << endl;// 2.实现逆置// 2.1 记录起始下标位置// 2.2 记录结束下标位置// 2.3 通过temp交换数据// 2.4 循环交换数据 条件是start < endint start = 0;int end = sizeof(arr) / sizeof(arr[0]) - 1;int temp;while (start < end) { //如果前面小标小于后面下标 则交换数据后前面下标往后移1格 后面下标往前移1格temp = arr[start];arr[start] = arr[end];arr[end] = temp;start++;end--;}// 3.打印逆置数组for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {cout << arr[i];}return 0;
}#include <stdio.h>
int main()
{int arr[] = {1,16,5,3,4,8,9};for(int i = 0;i<sizeof(arr)/sizeof(arr[0]);i++){printf("%d\t",arr[i]);}putchar('\n');int* tp = arr;int* wp = &arr[6];int temp;while(tp < wp){temp = *tp;*tp = *wp;*wp = temp;tp++;wp--;}for(int i = 0;i<sizeof(arr)/sizeof(arr[0]);i++){printf("%d\t",arr[i]);}putchar('\n');return 0;}
冒泡排序
#include <iostream>
using namespace std;
int main()
{// 1.冒泡排序 两两比较 比n-1轮,每轮比较n-i-1个数int arr[11] = {1,2,6,55,9,8,12,4,6,17,16 };for (int i = 0; i < 11; i++) {cout << arr[i] << endl;}// 2.冒泡排序for (int i = 0; i < 11 - 1; i++) { //比较n-1轮for (int j = 0; j < 11 - i - 1; j++) { //每轮比较 n-1-i个数if (arr[j] > arr[j + 1]) {int temp = arr[j + 1];arr[j + 1] = arr[j];arr[j] = temp;}}}for (int i = 0; i < 11; i++) {cout << arr[i] << endl;}//return 0;
}
二维数组的几种表示方法
#include <iostream>
using namespace std;
int main()
{// 1.二维数据的集中变现形式// 1.1int arr1[2][3] = { 1,2,3,4,5,6 };for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++) {cout << arr1[i][j]<< " ";}cout << endl;}// 1.2int arr2[2][3] = { {1,2,3},{4,5,6}};for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++) {cout << arr2[i][j] << " ";}cout << endl;}// 1.3int arr3[][3] = { 1,2,3,4,5,6};for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++) {cout << arr3[i][j] << " ";}cout << endl;}// 2.遍历二维数组return 0;
}
二维数组名作用
#include <iostream>
using namespace std;
int main()
{// 1.二维数组名即首地址,数组只写行即行的地址或者大小int arr[][3] = { {1,6,9},{4,1,0} };for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++) {cout << arr[i][j] << " ";}cout << endl;}cout << "二维数组占用内存为:" << sizeof(arr) << endl;cout << "二维数组第一行占用内存为:" << sizeof(arr[0]) << endl; //arr[0] 是第一行的大小和地址cout << "二维数组第一个元素占用内存为:" << sizeof(arr[0][0]) << endl;cout << "二维数组所占行数:" << sizeof(arr)/sizeof(arr[0]) << endl;;cout << "二维数组所占列数:" << sizeof(arr[0])/sizeof(arr[0][0]) << endl;cout << "二维数组首地址:" << arr << endl;cout << "二维数组第一行首地址:" << arr[0] << endl;cout << "二维数组第二行首地址:" << arr[1]<< endl;cout << "二维数组第一行元素首地址:" << &arr[0][0] << endl;cout << "二维数组第一行元素首地址:" << &arr[1][0] << endl;return 0;
}
空指针和野指针
空指针 :指针变量指向内存中编号为 0 的空间
用途: 初始化指针变量
注意: 空指针指向的内存是不可以访问的
#include <iostream>
using namespace std;
//调试会报错
int main()
{// 1.空指针:指针变量指向内存中编号为0的空间int* p = NULL;// 2.初始化指针变量cout << *p << endl;// 3.空指针指向的内存是不可以访问的 0-255不可访问*p = 100; cout << *p << endl;system("pause");return 0;
}#include <iostream>
using namespace std;int main()
{// 1.野指针 随意指向的一篇地址控制 没有权限访问 调试出错int* p = (int*)0x12345;cout << *p << endl;system("pause");return 0;
}
总结:空指针和野指针都不是我们申请的空间,因此不要访问。
const修饰指针
1. const 修饰指针 --- 常量指针
2. const 修饰常量 --- 指针常量
3. const 即修饰指针,又修饰常量
技巧:看 const 右侧紧跟着的是指针还是常量 , 是指针就是常量指针,是常量就是指针常量
#include <iostream>
using namespace std;int main()
{ //修饰谁 谁就不能改变// 1.const修饰指针 常量指针,指针指向可以修改,但是值不能修改int a = 10;int b = 20;const int* p1;// 常量指针p1 = &a;p1 = &b;// *p1 = a; 错误// 2.const修饰常量 指针常量,指向不能修改,值可以改变 int* const p2 = &a;*p2 = 100;// p2 = &b; 错误// 3.既修饰常量又修饰指针 两个都不能改变const int* const p3 = &a;// *p3 = 100; 错误// p3 = &b; 错误system("pause");return 0;
}
指针和函数
#include <iostream>
using namespace std;
//void swap01(int a, int b) {
// int temp;
// temp = a;
// a = b;
// b = temp;
// cout <<"swap01 a = "<< a << endl;
// cout << "swap01 b = " << b << endl;
//
//}void swap02(int* a, int* b) {int temp = *a;*a = *b;*b = temp; cout << "swap02 a = " << *a << endl;cout << "swap02 b = " << *b << endl;
}int main()
{// 指针和函数int a = 10;int b = 20;//swap01(a, b);//cout << "main a = " << a << endl;//cout << "main b = " << b << endl;swap02(&a, &b);cout << "main a = " << a << endl;cout << "main b = " << b << endl;system("pause");return 0;
}
指针函数数组小案例
#include <iostream>
using namespace std;void BubbleSort(int* arr, int len) {for (int i = 0; i < len; 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;}}}
}void PrintfArr(int* arr, int len) {for (int i = 0; i < len; i++) {cout << arr[i] << endl;}
}
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.打印数组函数PrintfArr(arr, len);system("pause");return 0;
}
结构体的三种定义
#include <iostream>
#include <string.h>
using namespace std;struct Student {string name;int age;int score;
}s3;int main()
{// 1.struct结构体名 变量名 = {x、x、x};// 2.结构体的三种表示形式// 2.1struct Student s1;s1.name = "张三";s1.age = 18;s1.score = 100;cout << s1.name <<"," << s1.age <<"," << s1.score << endl;// 2.2Student s2 = { "李四",19,80 };cout << s2.name << "," << s2.age << "," << s2.score << endl;// 2.3s3.name = "王五";s3.age = 17;s3.score = 60;cout << s3.name << "," << s3.age << "," << s3.score << endl;system("pause");return 0;
}
结构体数组
#include <iostream>
#include <string.h>
using namespace std;struct Student {string name;int age;int score;
};int main()
{// 1.创建结构体Student s[3] = { {"张三",18,90},{"李四",20,80},{"王五",70,50} };// 2.创建结构体数组// 3.修改结构体数组中的内容s[2].name = "赵六";s[2].age = 80;s[2].score = 60;// 4.遍历结构体数组for (int i = 0; i < 3; i++) {cout << " 姓名:" << s[i].name << " 年龄: " << s[i].age << " 成绩: " << s[i].score << endl;}system("pause");return 0;
}
结构体嵌套
#include <iostream>
#include <string.h>
using namespace std;struct Student {string name;int age;int score;
}; struct Teacher {string name;int age;int score;struct Student stu; //结构体嵌套
};int main()
{struct Teacher T = { "老王",60,98,{"小王",16,60} };cout << "老师名字: " << T.name << "老师年龄: " << T.age << "老师成绩: " << T.score << endl;cout << "老师带的学生名字: " << T.stu.name << "老师带的学生年龄: " << T.stu.age << "老师带的学生成绩: " << T.stu.score << endl;system("pause");return 0;
}
结构体做参数传递给函数
总结:如果不想修改主函数中的数据,用值传递,反之用地址传递
#include <iostream>
#include <string.h>
using namespace std;struct Student {string name;int age;int score;
};void PrintfStudent01(struct Student s) {s.age = 100;cout << "Student01 名字: " << s.name << " 年龄: " << s.age << " 成绩: " << s.score << endl;
}void PrintfStudent02(struct Student* p) {p->age = 200;cout << "Student02 名字: " << p->name << " 年龄: " << p->age << " 成绩: " << p->score << endl;
}
int main()
{// 1.结构体做参数传递给函数 分值传递(不能改变main中的值)、和地址传递(可以改变main中的值)Student s = { "啊浪",24,70 };PrintfStudent01(s); cout << "main 01 名字: " << s.name << " 年龄: " << s.age << " 成绩: " << s.score << endl;PrintfStudent02(&s);cout << "main 02 名字: " << s.name << " 年龄: " << s.age << " 成绩: " << s.score << endl;system("pause");return 0;
}
结构体中const 常量指针 防止修改数据
#include <iostream>
#include <string.h>
using namespace std;struct Student {string name;int age;int score;
};void PrintfStudent01(const struct Student* s) { //常量指针 可以修改指向,不能修改值//s->age = 100;cout << "Student01 名字: " << s->name << " 年龄: " << s->age << " 成绩: " << s->score << endl;
}int main()
{// 1.结构体做参数传递给函数 分值传递(不能改变main中的值)、和地址传递(可以改变main中的值)Student s = { "啊浪",24,70 };PrintfStudent01(&s);cout << "main 01 名字: " << s.name << " 年龄: " << s.age << " 成绩: " << s.score << endl;system("pause");return 0;
}
结构体案例1
//结构体案例
#include <iostream>
#include <string.h>
#include <ctime>
using namespace std;//学生
struct Student {string sName;int score;
};//老师
struct Teacher {string tName;struct Student sArry[5];
};//通过传过来老师数组的首地址 和长度对其赋值初始化
void mallocInfo(struct Teacher tArry[], int len) {int scoreRand;string Name = "ABCDE";for (int i = 0; i < len; i++) { //三个老师tArry[i].tName = "Teacher_";tArry[i].tName += Name[i];for (int j = 0; j < 5; j++) { //一个老师 五个学生scoreRand = rand() % 61 + 40; // 随机生成一个成绩随机数tArry[i].sArry[j].sName = "Student_";tArry[i].sArry[j].sName += Name[j];tArry[i].sArry[j].score = scoreRand;}}
}//打印老师和所带学生信息
void printfInfo(struct Teacher tArry[], int len) {for (int i = 0; i < len; i++) { //三个老师cout << "老师名字: " << tArry[i].tName << endl;for (int j = 0; j < 5; j++) { //一个老师 五个学生if (tArry[i].sArry[j].score > 60) {cout << "\t大于60分的学生名字: " << tArry[i].sArry[j].sName<< "学生分数: " << tArry[i].sArry[j].score << endl;}}}
}int main()
{// 生成随机数种子srand((unsigned int)time(NULL));// 1.创建老师数组 每个老师带五个学生struct Teacher tArry[3];// 2.对老师和学生赋值int len = sizeof(tArry) / sizeof(tArry[0]);mallocInfo(tArry, len);// 3.打印老师和学生的信息printfInfo(tArry, len);system("pause");return 0;
}
结构体案例2 三国
#include <iostream>
#include <string.h>
using namespace std;struct Person {string name;int age;string sex;
};//冒泡交换 升序
void bobling(struct Person people[], int len) {struct Person temp;for (int i = 0; i < len - 1; i++) {for (int j = 0; j < len - i - 1; j++) {if (people[j].age > people[j + 1].age) {//年龄升序temp = people[j]; //交换people[j] = people[j + 1];people[j + 1] = temp;}}}
}//打印函数
void printfPeople(struct Person people[], int len) {for (int i = 0; i < len; i++) {cout << people[i].name << "," << people[i].age << "," << people[i].sex << endl;}
}int main()
{// 1.定义一个结构体数组struct Person people[] = { {"吕布",35,"Man"},{"赵云",30,"Man"},{"关羽",33,"Man"},{"张飞",31,"Man"},{"貂蝉",16,"Women"} };int len = sizeof(people) / sizeof(people[0]);// 2.通过结构体数组中的年龄进行升序bobling(people, len);// 3.打印出结构体内容printfPeople(people, len);system("pause");return 0;
}
这篇关于C++ 路漫漫其修远兮的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!