c语言中swap函数_C ++中的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,

语法


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,

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


#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

    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.

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


#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:

输出

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()函数交换数组 。 让我们看看


#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:

输出

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


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

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

结论 (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函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值