c语言中swap函数
In this tutorial, we are going to learn the swap() function in C++ programming language. Swapping is a simple operation in C++ which basically is the exchange of data or values among two variables of any data type. Considering both the variables are of the same data type.
在本教程中,我们将学习C ++编程语言中的swap()函数。 交换是C ++中的一个简单操作,基本上是在任何数据类型的两个变量之间交换数据或值。 考虑到两个变量的数据类型相同。
So, let us get right into the function and its working.
因此,让我们直接了解该函数及其功能。
The function in C++, from the standard library, is a function that directly swaps values between two given variables of the same types. Let us look at the syntax for using the same:
来自标准库的C ++中的函数是一个在两个相同类型的给定变量之间直接交换值的函数。 让我们看看使用相同的语法:
Syntax,
语法
Here:
这里:
- a and b, both are variables of the same data type, a和b都是相同数据类型的变量,
- We pass both of them to the function and the values at their respective addresses are exchanged among themselves, 我们将它们都传递给函数,并且它们各自地址处的值相互交换 ,
- a now stores the value b previously had, whereas, b has the value which a prior to swapping stored. 一个现在存储值b以前有,反之,b的交换存储在之前,它的值。
Now, let us see how we can use the function in C++.
现在,让我们看看如何在C ++中使用函数。
As we specified earlier, the function from the standard library in C++ can swap values of any data type including int, float, string, etc. and even data structures like arrays, stacks, and queues, etc.
正如我们之前所指定的, C ++中标准库中的函数可以交换任何数据类型的值,包括int,float,string等,甚至数据结构,如数组,堆栈和队列等。
So now we are going to look at some examples where we try to swap integers, strings as well as arrays in order to have a clear understanding.
因此,现在我们来看一些示例,在这些示例中我们尝试交换整数 , 字符串以及数组 ,以使您有一个清晰的认识。
1.在C ++中使用swap()交换两个整数值 (1. Swapping two integer values using swap() in C++)
First, let us consider the swapping of two integer values using the function. Look at the code given below carefully,
首先,让我们考虑使用函数交换两个整数值。 仔细看看下面给出的代码,
Output:
输出 :
Here,
这里,
- and are initialized with values 2 and 5 respectively 和分别用值2和5初始化
- After we pass both of them to the function the values of the same are printed to confirm the swapping 在将它们都传递给函数之后,将打印它们的值以确认交换
- As we can see, val1 now holds 5 whereas, val2 stores 2. That is, we successfully swapped both the values 如我们所见, val1现在保存5,而val2存储2 。 也就是说,我们成功地交换了两个值
2.使用swap()交换两个字符串 (2. Swapping of two strings using swap())
Now, let us see how we can swap two string objects from the string header file.
现在,让我们看看如何从字符串头文件中交换两个字符串对象。
Output:
输出 :

Clearly, from the output given below, our swapping is successful. Both the strings and values are exchanged.
显然,从下面给出的输出中,我们的交换是成功的。 字符串和值都被交换。
3.使用swap()交换两个array() (3. Swapping two arrays() using swap())
We can even swap arrays using the function. Let us see how
我们甚至可以使用函数交换数组 。 让我们看看
Output:
输出 :

In the code above:
在上面的代码中:
- array1 and array2 are the two given arrays array1和array2是两个给定的数组
- We pass both the arrays to the function for swapping 我们将两个数组都传递给函数进行交换
- After swapping, as we can see from the output, the contents of both the arrays are now exchanged 交换之后,从输出中可以看到,两个数组的内容现在都已交换
Note: Here we have used examples of swapping integers, strings and arrays for clear understanding. But the functions swapping ability is not limited to these data types. We can also swap other data structures like stacks and queues and other data types
注意 :这里我们使用交换整数,字符串和数组的示例来进行清楚的理解。 但功能交换能力并不局限于这些数据类型。 我们还可以交换其他数据结构,例如堆栈和队列以及其他数据类型
While using the function if we try to swap the values of two variables belonging to two different data types. The following error is thrown by the compiler.
在使用函数时,如果我们尝试交换属于两种不同数据类型的两个变量的值。 编译器将引发以下错误。
Error:
错误 :
Hence, for avoiding this type of conflict, we must make sure that the data types of the variables are the same.
因此,为了避免这种类型的冲突,我们必须确保变量的数据类型相同。
Hope this tutorial helps to understand the use as well as the working of the function in C++. For any questions feel free to use the comments below.
希望本教程有助于理解C ++中 函数的用法和工作原理 。 如有任何疑问,请随时使用以下评论。
- Stackoverflow question on simple swap function in C++, 关于C ++中简单交换函数的 Stackoverflow问题,
- Stackoverflow question on How does the standard library implement std::swap() in C++ 关于标准库如何在C ++中实现std :: swap()的 Stackoverflow问题
翻译自: https://www.journaldev.com/37269/swap-function-in-c-plus-plus
c语言中swap函数
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.mushiming.com/mjsbk/14298.html