关于SWAP函数举例,指针实例

来源:互联网 发布:js object属性 编辑:程序博客网 时间:2024/06/04 17:51

关于SWAP函数举例,指针实例:


交换两个整型数的方法

交换两个整型数是C/C++中最常见的操作。

实现这个操作的方法很多。

最基本的方法就是使用一个临时变量,具体的代码如下:

int a,b;
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;
}
复制代码

 另外,还经常出现的一种情况是不使用临时变量来交换两个整型数,一般常见的方法有两种:加法和异或运算,具体如下表所示:

复制代码
void swap1(int& x,int& y)
{
x=x+y;
y=x-y;
x=x-y;
}
复制代码
复制代码
void swap2(int &x,int &y)
{
x=x-y;
y=x+y;
x=y-x;
}
复制代码
复制代码
void swap3(int& x,int& y)
{
x ^= y;
y ^= x;
x ^= y;
}
复制代码

x和y同号的情况下容易溢出

x和y异号的情况下容易溢出

 

左边的两种交换也存在问题就是整数的溢出。

还有一种情况就是输入是swap(a,a)的情况。这样的话就会出问题。

所以更严谨的做法如下:

复制代码
void swap4(int &x,int &y)
{
if(x==y)
return ;
if((x>0&&y>0)||(x<0&&y<0)) {
x=x-y;
y=x+y;
x=y-x;
}
else{
x=x+y;
y=x-y;
x=x-y;
}
}
复制代码
复制代码
void swap5(int &x,int &y)
{
if(x==y)
return;
x^=y;
y^=x;
x^=y;
}
复制代码
复制代码
void swap7(int &x,int &y)
{
if(x==y)
return;
y=x+y-(x=y);
}
复制代码

 引申:

在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函数的。

 下面是一些简单的例子:

 

复制代码
// swap strings
#include <iostream>
#include <string>
using namespace std;
main ()
{
  string buyer ("money");
  string seller ("goods");

  cout << "Before swap, buyer has " << buyer;
  cout << " and seller has " << seller << endl;

  seller.swap (buyer);

  cout << " After swap, buyer has " << buyer;
  cout << " and seller has " << seller << endl;
//Before swap, buyer has money and seller has goods
//After swap, buyer has goods and seller has money
  return 0;
}
复制代码
复制代码
// swap vectors
#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
复制代码

0 0