In C ++ swap

In C ++ swap (swap function)

The exchange value of the two variables is simple.

Such int a = 1; b = 2; ab value of the exchange

This is very simple to find a readily conceivable, such as intermediate variable int temp = a; a = b; b = temp;

You can not require intermediate variables?

Of course it is possible.

such as

【Addition and Subtraction】

a = a + b; b = a - b; a = a - b;

The method may exchange integer and floating-point values ​​of the variables, but may in dealing with the loss of precision floating-point occurs, for example data:

a = 3.123456

b = 1234567.000000

After the exchange value of each variable becomes:

a = 1234567.000000

b = 3.125000

Obviously, the original value of a loss of precision occurs during the exchange of a b.

[] Multiplication and division

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

 

Like multiplication and division are mapped to the addition and subtraction, multiplication and division operations, which is similar to subtraction: can handle integer and floating point variables, but in the floating point variable also exists

Loss of accuracy problems. Subtraction and multiplication and division to be more than one constraint: b is 0 will not.

 Probably on some kind of intuitive experience tells us: addition and subtraction and multiplication and division may overflow, multiplication and division and overflow will be particularly severe. In actual fact, the use of these two

 The method will not overflow. With addition and subtraction, for example, the first step of the add operation may cause overflow, but it causes an overflow of the overflow will be back in the subtraction of behind.

XOR [method]

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

XOR method can be done in exchange for integer variables, floating-point type for the variable it can not complete the exchange.

The second type method is more like playing a word game, this method uses a method of embedding in the code assembly code avoids the introduction of temporary variables, but their

Nature will still use extra storage space. This method can have a variety, a few are listed below:

and many more..............

But the structure of this exchange should be less practical need for every piece of data to be exchanged for the structure, this time a function is the most simple.

C ++ provides a function to swap exchange, is used as follows.

 swap included in the namespace std inside

 

#Include. 1 <the iostream> 
 2 #include <String> 
 . 3 #include <algorithm> header file contains // sort function 
 . 4 the using namespace STD; 
 . 5 // student defines a structure type 
 . 6 typedef struct Student 
 . 7 { 
 . 8 String name ; // name of student 
 9 int achievement; // student performance 
10 student}; 
. 11 
12 is 
13 is 
14 
15 // function for displaying student information 
16 void Show (student * STU, n-int) 
. 17 { 
18 is for (int I = 0; I <n-; I ++) 
. 19 { 
20 is COUT << "name:" << stu [i] .name << '\ t' << " results:" << stu [i] .achievement << endl; 
} 21 is 
22 is} 
23 is 
24 int main () 
25 {
; 26 student stu [] = { { " John Doe", 99}, { "John Doe", 87}, { "king", 100}, { "pockmarks", 60}} 
front 27 cout << "exchange : "<< endl; 
28 Show (STU,. 4); 
29 the swap (STU [0], STU [. 3]); 
30 COUT <<" after exchange: "<< endl; 
31 is Show (STU,. 4); 
32 0 return; 
33 is}

Copy the code

 Do not worry about the loss of function with precision 

Copy the code

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     float a = 3.123456,b = 1234567.000000;
 6     swap(a,b);
 7     cout<<fixed;
 8     cout<<a<<"->"<<b<<endl;
 9     return 0;
10 }

Copy the code

 

Copy the code

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main()
 5 {
 6     string a ="666",b = "999";
 7     swap(a,b);
 8     cout<<a<<"->"<<b<<endl;
 9     return 0;
10 }
Published 18 original articles · won praise 86 · views 160 000 +

Guess you like

Origin blog.csdn.net/u013178472/article/details/104968806