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

关于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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值