当前位置:   article > 正文

c语言中swap函数_C ++中的swap()函数

swap()

c语言中swap函数

介绍 (Introduction)

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.

因此,让我们直接了解该函数及其功能。

在C ++中swap()函数的工作 (Working of the swap() function in C++)

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,

语法

  1. std::swap( a , b );

Here:

这里:

  • a and b, both are variables of the same data type,

    ab都是相同数据类型的变量,
  • We pass both of them to the swap() function and the values at their respective addresses are exchanged among themselves,

    我们将它们都传递给swap()函数,并且它们各自地址处的值相互交换
  • 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 swap() function in C++.

现在,让我们看看如何在C ++中使用swap()函数。

如何在C ++中使用swap()函数 (How to use the swap() function in C++)

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.

因此,现在我们来看一些示例,在这些示例中我们尝试交换整数字符串以及数组 ,以使您有一个清晰的认识。

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,

首先,让我们考虑使用函数交换两个整数值。 仔细看看下面给出的代码,

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. //values before swapping
  6. int val1 = 2;
  7. int val2 = 5;
  8. //swapping using swap()
  9. swap(val1, val2);
  10. //values after swapping
  11. cout<<"New value of val1 = "<<val1<<endl;
  12. cout<<"New value of val2 = "<<val2<<endl;
  13. return 0;
  14. }

Output:

输出

  1. New value of val1 = 5
  2. New value of val2 = 2

Here,

这里,

  • val1 and val2 are initialized with values 2 and 5 respectively

    val1val2分别用值2和5初始化
  • After we pass both of them to the std::swap() function the values of the same are printed to confirm the swapping

    在将它们都传递给std::swap()函数之后,将打印它们的值以确认交换
  • 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.

现在,让我们看看如何从字符串头文件中交换两个字符串对象。

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. //strings before swapping
  7. string string1 = "String 1";
  8. string string2 = "String 2";
  9. //swapping strings using swap()
  10. swap(string1, string2);
  11. //strings after swapping
  12. cout<<"New value of string1 = "<<string1<<endl;
  13. cout<<"New value of string2 = "<<string2<<endl;
  14. return 0;
  15. }

Output:

输出

String Swapping using Swap() function in C++
String Swapping
字符串交换

Clearly, from the output given below, our swapping is successful. Both the strings string1 and string2 values are exchanged.

显然,从下面给出的输出中,我们的交换是成功的。 字符串string1string2值都被交换。

3.使用swap()交换两个array() (3. Swapping two arrays() using swap())

We can even swap arrays using the swap() function. Let us see how

我们甚至可以使用swap()函数交换数组 。 让我们看看

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. //arrays before swapping
  6. int array1[3] = {1,2,3};
  7. int array2[3] = {2,4,6};
  8. int i;
  9. //swapping arrays using swap()
  10. swap(array1, array2);
  11. //arrays after swapping
  12. cout<<"New value of array1 = "<<endl;
  13. for(i=0;i<3;i++)
  14. cout<<" "<<array1[i];
  15. cout<<"\nNew value of array2 = "<<endl;
  16. for(i=0;i<3;i++)
  17. cout<<" "<<array2[i];
  18. return 0;
  19. }

Output:

输出

Swapping Arrays
Swapping Arrays
交换阵列

In the code above:

在上面的代码中:

  • array1 and array2 are the two given arrays

    array1array2是两个给定的数组
  • We pass both the arrays to the swap() function for swapping

    我们将两个数组都传递给swap()函数进行交换
  • 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

注意 :这里我们使用交换整数,字符串和数组的示例来进行清楚的理解。 但功能交换能力并不局限于这些数据类型。 我们还可以交换其他数据结构,例如堆栈队列以及其他数据类型

在C ++中使用swap()函数时出错 (Error while using the swap() function in C++)

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()函数时,如果我们尝试交换属于两种不同数据类型的两个变量的值。 编译器将引发以下错误。

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. //arrays before swapping
  6. int array1[3] = {1,2,3};
  7. char array2[3] = {'1','2','3'};
  8. int i;
  9. //swapping arrays using swap() we get error here
  10. swap( array1, array2);
  11. //arrays after swapping
  12. cout<<"New value of array1 = "<<endl;
  13. for(i=0;i<3;i++)
  14. cout<<" "<<array1[i];
  15. cout<<"\nNew value of array2 = "<<endl;
  16. for(i=0;i<3;i++)
  17. cout<<" "<<array2[i];
  18. return 0;
  19. }

Error:

错误

  1. 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.

因此,为了避免这种类型的冲突,我们必须确保变量的数据类型相同。

结论 (Conclusion)

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()函数的用法和工作原理 。 如有任何疑问,请随时使用以下评论。

参考资料 (References)

翻译自: https://www.journaldev.com/37269/swap-function-in-c-plus-plus

c语言中swap函数

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/1016524
推荐阅读
  

闽ICP备14008679号