当前位置: 首页 > news >正文

C++使用技巧(二十七):回顾函数指针参数、数组参数、结构体函数参数

目录

  • 1、函数指针参数
  • 2、数组参数 冒泡排序函数
  • 3、结构体、结构体指针、嵌套结构体、结构体做函数参数

1、函数指针参数

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h> 
using namespace std;
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); // 值传递不会改变实参
    cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	swap2(&a, &b); //地址传递会改变实参
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	system("pause");

	return 0;
}

结果:
a = 10
b = 20
a = 20
b = 10
总结:如果不想修改实参,就用值传递,如果想修改实参,就用地址传递

2、数组参数 冒泡排序函数

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h> 
using namespace std;
//冒泡排序函数
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;
}

// 总结:当数组名传入到函数作为参数时,被退化为指向首元素的指针

3、结构体、结构体指针、嵌套结构体、结构体做函数参数

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

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

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

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h> 
using namespace std;
//结构体定义
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;
}

姓名:张三 年龄:18 分数:100
姓名:李四 年龄:19 分数:60
姓名:王五 年龄:18 分数:80

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h> 
using namespace std;
//结构体定义
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;
}

姓名:张三 年龄:18 分数:80
总结:结构体指针可以通过 -> 操作符 来访问结构体中的成员

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h> 
using namespace std;
//学生结构体定义
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;
}


教师 职工编号: 10000 姓名: 老王 年龄: 40
辅导学员 姓名: 张三 年龄:18 考试分数: 100
嵌套结构体:
**总结:**在结构体中可以定义另一个结构体作为成员,用来解决实际问题

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <time.h> 
using namespace std;
//学生结构体定义
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

总结:如果不想修改主函数中的数据,用值传递,反之用地址传递

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • C++使用技巧(二十八):回顾内存new关键字、引用、默认参数、 占位参数、重载、类和对象、构造函数
  • 无组件上传图片到数据库源码
  • 关于 PyTorch android 在应用程序中的使用示例
  • 完美解决ubuntu报错:ppa.launchpad与404 Not Found [IP: 91.189.95.85 80]
  • AI模型C++部署:ubuntu安装Cython并使用C/C++调用python动态库【附加c++与python互相调用算法demo程序接口的源码】
  • Java学习备忘录(四)方法篇(原创)
  • AI服务器环境:OpenCV++与spleeter人声音伴奏分离docker环境/源码地址
  • AI模型C++部署:【配置OpenCV4++环境】与【三种在 C++ 中部署 TensorFlow 模型的方式】【准备阶段】
  • 一个文章在线编辑器的实现
  • AI模型C++部署:TensorFlow2图像分类模型之金钱豹大战齐天大圣【OpenCV纯C++接口调用tensorflow生成的pb模型】【源码已开源】
  • 失败得要命,我想我还是太嫩了。
  • 目标检测系列算法复现1:Darknet-YOLO-CUDA11-OpenCV4(Ubuntu平台)
  • 一个Java读取串口值的类(收藏)
  • 目标检测系列算法复现2:Darknet-YOLO-CUDA11-OpenCV4(Ubuntu平台)推理测试
  • AI模型部署到Android端:模拟器App的生成与tensorflow模型的输入输出调试
  • 【译】理解JavaScript:new 关键字
  • Android系统模拟器绘制实现概述
  • Effective Java 笔记(一)
  • ES学习笔记(12)--Symbol
  • LeetCode18.四数之和 JavaScript
  • LeetCode29.两数相除 JavaScript
  • rc-form之最单纯情况
  • webpack4 一点通
  • 分享自己折腾多时的一套 vue 组件 --we-vue
  • 基于 Babel 的 npm 包最小化设置
  • 数据结构java版之冒泡排序及优化
  • 通过npm或yarn自动生成vue组件
  • 物联网链路协议
  • 小程序 setData 学问多
  • ​14:00面试,14:06就出来了,问的问题有点变态。。。
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • #### go map 底层结构 ####
  • #大学#套接字
  • (06)Hive——正则表达式
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (leetcode学习)236. 二叉树的最近公共祖先
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (八)Flask之app.route装饰器函数的参数
  • (超详细)语音信号处理之特征提取
  • (二)测试工具
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (五)网络优化与超参数选择--九五小庞
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转)Unity3DUnity3D在android下调试
  • *算法训练(leetcode)第三十九天 | 115. 不同的子序列、583. 两个字符串的删除操作、72. 编辑距离
  • .apk文件,IIS不支持下载解决
  • .Net Core 生成管理员权限的应用程序
  • .NET Framework .NET Core与 .NET 的区别
  • .NET MAUI Sqlite数据库操作(二)异步初始化方法
  • .NET 材料检测系统崩溃分析
  • .NET 中的轻量级线程安全
  • //TODO 注释的作用
  • /bin、/sbin、/usr/bin、/usr/sbin
  • @Autowired标签与 @Resource标签 的区别
  • @Data注解的作用