Swap (swap function) in C + +

Source: Internet
Author: User

Exchanging the values of two variables is simple.

such as int a = 1; b = 2; Exchange value of a B

It's easy to think of a middle variable like int temp = A; A = b; b = temp;

Don't need intermediate variables?

Of course it's possible.

Like what

"Add and Subtract"

A = a + B; b = a A; A = a -B;

This method can exchange integer and floating-point numeric variables, but it is possible to lose precision when dealing with floating-point types, such as data:

A = 3.123456

b = 1234567.000000

After swapping, the variable values change to:

A = 1234567.000000

b = 3.125000

Obviously, the value of a has lost precision in the process of exchanging to B.

"Multiplication Law"

A = a * b; b = A/b; A = A/b;

The multiplication method is more like a mapping of addition and subtraction to the multiplication operation, similar to addition and subtraction: it can handle integer and floating-point variables, but also when handling floating-point variables

The problem of loss of precision. and multiplication Fabiga subtraction to one more constraint: B must not be 0.

Perhaps some of the experience's intuition tells us that the addition and subtraction and multiplication methods may overflow, and that multiplication overflow can be particularly severe. In fact, the use of these two

method does not overflow. Add and subtract for example, the first step of the addition operation may cause overflow, but it caused the overflow will be in the back of the subtraction operation is overrun.

"XOR Law"

a ^= b; A=a^b

b ^= A; b=b^ (a^b) =b^a^b=b^b^a=0^a=a

a ^= b; A= (a^b) ^a=a^b^a=a^a^b=0^b=b

The Xor method can complete the exchange of integer variables, and it cannot complete the interchange for floating-point variables.

The second kind of approach is more like playing a word game, which uses the method of embedding assembly code in code to avoid the introduction of temporary variables, but the

In essence, additional storage space is used. There are several types of this approach, which are listed below:

Wait a minute..............

But the exchange of structures is not very practical, it should be for the structure of each data must be exchanged, this time with the function is the simplest.

C + + provides a swap function for swapping with the following usage.

Swap included in namespace Std

1#include <iostream>2#include <string>3#include <algorithm>//the Sort function contains the header file4 using namespacestd;5 //define the structure of a student type6typedefstructStudent7 {8     stringName//Student Name9     intachievement;//Student AchievementTen } student; One  A  -  -  the //a function to display student information - voidShow (Student *stu,intN) - { -      for(inti =0; I < n; i++) +     { -cout<<"Name:"<<stu[i].name<<'\ t'<<"Score:"<<stu[i].achievement<<Endl; +     } A } at  - intMain () - { -Student stu[] = {{"Zhang San", About},{"John Doe", the},{"Wang er", -} ,{"Leper", -}}; -cout<<"before Exchange:"<<Endl; -Show (Stu,4); inSwap (stu[0],stu[3]); -cout<<"after Exchange:"<<Endl; toShow (Stu,4); +     return 0; -}

use the function without worrying about the loss of precision  

1#include <iostream>2 using namespacestd;3 intMain ()4 {5     floatA =3.123456, B =1234567.000000;6 Swap (A, b);7cout<<fixed;8cout<<a<<" -"<<b<<Endl;9     return 0;Ten}

1#include <iostream>2#include <string>3 using namespacestd;4 intMain ()5 {6     stringA ="666", B ="999";7 Swap (A, b);8cout<<a<<" -"<<b<<Endl;9     return 0;Ten}

Swap (swap function) in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.