Three methods about swap function in C/C++

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


Preface

When learning programming, the exchange function is an example that we must understand. Here are some of my understandings, I hope it can help everyone.


1. Description of exchange function

Create two values ​​in the main function and swap them by calling the function.

2. Explanation of the three situations

1. Value transfer

The code is as follows (example):

#include<iostream>
using namespace std;
//1.值传递
void Swap1(int x,int y)
{
    int temp = x;     //定义中间变量,用于交换两个数值的中间媒介
    x = y;
    y = temp;
}
int main()
{
    int a = 20, b = 30;
    Swap1(a,b);        //调用交换函数1
    cout << "a=" << a << endl;
    cout << "b=" << b << endl;
    system("pause");
    return 0;
}

1.1值传递的运行结果

It can be seen that we want to exchange the values ​​​​of the two functions, but the exchange is not implemented. Why is this?

As can be seen from the figure, because: the value-passing function does not change the values ​​of the actual parameters (a, b), but only changes the values ​​of the formal parameters (in order to verify, we can add an output statement to the function to verify whether the formal parameters have changed)

 

 It can be seen that the formal parameters have been changed. When the function call ends, the corresponding memory of x and y will be cleared. After that, the code continues to execute sequentially and returns to the main function to execute the output statement.

 2. Call by address

#include<iostream>
using namespace std;
//2.地址传递
void Swap1(int *p1,int *p2)
{
	int temp = *p1;
	*p1 = *p2;
	*p2 = temp;
	//cout << "x=" << *p1 << endl;
	//cout << "y=" << *p2 << endl;
}
int main()
{
	int a = 20, b = 30;
	Swap1(&a,&b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}

operation result:

 As can be seen from the figure, call by address realizes the exchange of two values. Swap1(&a,&b), passes the address, so we use a pointer to receive void Swap1(int *p1, int *p2). It can be understood that pointer p1 points to the memory of a, and pointer p2 points to the memory of b. *p1 and *p2 operate on the memory data of a and b and change the values ​​of the actual parameters, so the values ​​of a and b are exchanged after the function is called.

3. Reference as function parameter

#include<iostream>
using namespace std;
//3.引用作为函数参数
void Swap1(int &x,int &y)
{
	int temp=x;
	x = y;
	y = temp;
}
int main()
{
	int a = 20, b = 30;
	Swap1(a,b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}

operation result:

 As can be seen from the figure, the exchange of two numbers is realized. The reference can be understood as giving a an "alias" and can simultaneously operate an address a and x can simultaneously operate the data in this memory. So the actual parameters in memory will be changed regardless of whether the formal parameters are released.

 Description of reference variables

#include<iostream>
using namespace std;
//对于引用变量的解释
int main()
{
	int a = 10;
//创建引用变量的语法:“ 数据类型 &别名=原名;”
	int& b = a; //a和 b可以同时操作a这块内存的数值
	int c = 20;
	b = c;
	cout <<"a=" << a << endl;
	system("pause");
	return 0;
}


Summarize

Share my understanding of these three methods, I hope it will be helpful to everyone. If there is something incorrect or inaccurate, I hope you guys can leave a message and give me some pointers. Thank you.

Guess you like

Origin blog.csdn.net/m0_74058637/article/details/129172674