赞
踩
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 swap()
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 ++中的swap()
函数是一个在两个相同类型的给定变量之间直接交换值的函数。 让我们看看使用相同的语法:
Syntax,
语法
-
- std::swap( a , b );
Here:
这里:
swap()
function and the values at their respective addresses are exchanged among themselves, 我们将它们都传递给swap()
函数,并且它们各自地址处的值相互交换 , Now, let us see how we can use the swap()
function in C++.
现在,让我们看看如何在C ++中使用swap()
函数。
As we specified earlier, the swap()
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 ++中标准库中的swap()
函数可以交换任何数据类型的值,包括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.
因此,现在我们来看一些示例,在这些示例中我们尝试交换整数 , 字符串以及数组 ,以使您有一个清晰的认识。
First, let us consider the swapping of two integer values using the function. Look at the code given below carefully,
首先,让我们考虑使用函数交换两个整数值。 仔细看看下面给出的代码,
-
- #include <iostream>
- using namespace std;
- int main()
- {
- //values before swapping
- int val1 = 2;
- int val2 = 5;
-
- //swapping using swap()
- swap(val1, val2);
-
- //values after swapping
- cout<<"New value of val1 = "<<val1<<endl;
- cout<<"New value of val2 = "<<val2<<endl;
-
- return 0;
- }
Output:
输出 :
-
- New value of val1 = 5
- New value of val2 = 2
Here,
这里,
val1
and val2
are initialized with values 2 and 5 respectively val1
和val2
分别用值2和5初始化 std::swap()
function the values of the same are printed to confirm the swapping 在将它们都传递给std::swap()
函数之后,将打印它们的值以确认交换 Now, let us see how we can swap two string objects from the string header file.
现在,让我们看看如何从字符串头文件中交换两个字符串对象。
-
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- //strings before swapping
- string string1 = "String 1";
- string string2 = "String 2";
-
-
- //swapping strings using swap()
- swap(string1, string2);
-
- //strings after swapping
- cout<<"New value of string1 = "<<string1<<endl;
- cout<<"New value of string2 = "<<string2<<endl;
-
- return 0;
- }
Output:
输出 :
Clearly, from the output given below, our swapping is successful. Both the strings string1
and string2
values are exchanged.
显然,从下面给出的输出中,我们的交换是成功的。 字符串string1
和string2
值都被交换。
We can even swap arrays using the swap()
function. Let us see how
我们甚至可以使用swap()
函数交换数组 。 让我们看看
-
- #include <iostream>
- using namespace std;
- int main()
- {
- //arrays before swapping
- int array1[3] = {1,2,3};
- int array2[3] = {2,4,6};
- int i;
-
- //swapping arrays using swap()
- swap(array1, array2);
-
- //arrays after swapping
- cout<<"New value of array1 = "<<endl;
- for(i=0;i<3;i++)
- cout<<" "<<array1[i];
-
- cout<<"\nNew value of array2 = "<<endl;
- for(i=0;i<3;i++)
- cout<<" "<<array2[i];
-
- return 0;
- }
Output:
输出 :
In the code above:
在上面的代码中:
swap()
function for swapping 我们将两个数组都传递给swap()
函数进行交换 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 swap()
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.
在使用swap()
函数时,如果我们尝试交换属于两种不同数据类型的两个变量的值。 编译器将引发以下错误。
-
- #include <iostream>
- using namespace std;
- int main()
- {
- //arrays before swapping
- int array1[3] = {1,2,3};
- char array2[3] = {'1','2','3'};
- int i;
-
- //swapping arrays using swap() we get error here
- swap( array1, array2);
-
- //arrays after swapping
- cout<<"New value of array1 = "<<endl;
- for(i=0;i<3;i++)
- cout<<" "<<array1[i];
-
- cout<<"\nNew value of array2 = "<<endl;
- for(i=0;i<3;i++)
- cout<<" "<<array2[i];
-
- return 0;
- }
Error:
错误 :
-
- C:\Users\sneha\Desktop\C++.cpp [Error] no matching function for call to 'swap(int [3], char [3])'
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 swap()
function in C++. For any questions feel free to use the comments below.
希望本教程有助于理解C ++中 swap()
函数的用法和工作原理 。 如有任何疑问,请随时使用以下评论。
翻译自: https://www.journaldev.com/37269/swap-function-in-c-plus-plus
c语言中swap函数
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。