交換兩個整型數是C/C++中最常見的操作。
實現這個操作的方法很多。
最基本的方法就是使用一個臨時變量,具體的代碼如下:
int tmp;
tmp=a;
a=b;
b=tmp;
如果以函數的形式寫出來的話就是:
void swap(int *a,int *b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
在C++中,可以使用引用來實現的比較優雅:
void swap(int& a,int &b)
{
int tmp;
tmp=a;
a=b;
b=tmp;
}
另外,還經常出現的一種情況是不使用臨時變量來交換兩個整型數,一般常見的方法有兩種:加法和異或運算,具體如下表所示:
|
|
|
x和y同號的情況下容易溢出 |
x和y異號的情況下容易溢出 |
左邊的兩種交換也存在問題就是整數的溢出。
還有一種情況就是輸入是swap(a,a)的情況。這樣的話就會出問題。
所以更嚴謹的做法如下:
|
|
|
引申:
在C++中支持模板操作,所以,可以利用這個寫一個通用的swap操作:
template <class T>
void swap ( T& a, T& b )
{
T c(a);
a=b;
b=c;
}
這個其實是C++標準模板庫中函數。該函數可以交換任意兩個類型:
// swap algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int x=10, y=20; // x:10 y:20
swap(x,y); // x:20 y:10
vector<int> first (4,x), second (6,y); // first:4x20 second:6x10
swap(first,second); // first:6x10 second:4x20
cout << "first contains:";
for (vector<int>::iterator it=first.begin(); it!=first.end(); ++it)
cout << " " << *it;
//first contains: 10 10 10 10 10 10
cout << endl;
return 0;
}
除此之外,在標準C++中string,vector,map,set等容器都是有swap函數的。
下面是一些簡單的例子:
|
#include <iostream>
#include <vector> using namespace std; int main () { unsigned int i; vector<int> first (3,100); // three ints with a value of 100 vector<int> second (5,200); // five ints with a value of 200 first.swap(second); cout << "first contains:"; for (i=0; i<first.size(); i++) cout << " " << first[i]; cout << "\nsecond contains:"; for (i=0; i<second.size(); i++) cout << " " << second[i]; //first contains: 200 200 200 200 200 //second contains: 100 100 100 cout << endl; return 0; } |
// swap maps
#include <iostream> #include <map> using namespace std; int main () { map<char,int> foo; map<char,int> bar; map<char,int>::iterator it; foo['x']=100; foo['y']=200; bar['a']=11; bar['b']=22; bar['c']=33; foo.swap(bar); cout << "foo contains:\n"; for ( it=foo.begin() ; it != foo.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; cout << "bar contains:\n"; for ( it=bar.begin() ; it != bar.end(); it++ ) cout << (*it).first << " => " << (*it).second << endl; return 0; } foo contains: a => 11 b => 22 c => 33 bar contains: x => 100 y => 200 |
// swap sets
#include <iostream> #include <set> using namespace std; main () { int myints[]={12,75,10,32,20,25}; set<int> first (myints,myints+3); // 10,12,75 set<int> second (myints+3,myints+6); // 20,25,32 set<int>::iterator it; first.swap(second); cout << "first contains:"; for (it=first.begin(); it!=first.end(); it++) cout << " " << *it; cout << "\nsecond contains:"; for (it=second.begin(); it!=second.end(); it++) cout << " " << *it; cout << endl; return 0; } first contains: 20 25 32 second contains: 10 12 75 |
另外,還有不使用臨時變量交換N個整型數的操作,
| a | b | c | d | e |
+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+
| a | b | c | d | e |
+---+---+---+---+---+
| 2 | 3 | 4 | 5 | 1 |
+---+---+---+---+---+
|
然後可以把代碼優化爲:
|
繼續優化,把三句壓縮爲一句,如下:
|
還可再優化,如下:
|
現在來順次交換5個變量的值,如下:
|
既然有返回值,那麼可以寫成鏈式的,如下:
|
現在,讓我們來把swap函數依次用相應的函數體替換掉,如下:
|
好了,最後一個語句就實現了順次交換五個變量的值,寫程序驗證如下:
#define PRINT(A) do {/
printf("%d/n", A);/
} while (0)
int main()
{
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
// b ^= a ^= b ^= a; // swap(a, b)
// c ^= b ^= c ^= b; // swap(b, c)
// d ^= c ^= d ^= c; // swap(c, d)
// e ^= d ^= e ^= d; // swap(d, e)
e ^= d ^= e ^= d ^= c ^= d ^= c ^= b ^= c ^= b ^= a ^= b ^= a;
PRINT(a);
PRINT(b);
PRINT(c);
PRINT(d);
PRINT(e);
return 0;
}