关于SWAP函数举例,指针实例
2016-06-07 11:15
507 查看
关于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;
}
另外,还经常出现的一种情况是不使用临时变量来交换两个整型数,一般常见的方法有两种:加法和异或运算,具体如下表所示:
左边的两种交换也存在问题就是整数的溢出。
还有一种情况就是输入是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函数的。
下面是一些简单的例子:
交换两个整型数的方法
交换两个整型数是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 |
相关文章推荐
- 关于指针的一些事情
- C# Pointer指针应用实例简述
- C++智能指针实例详解
- C++指向函数的指针实例解析
- 关于c语言指针的两处小tip分享
- 浅谈c/c++中使用指针需要注意的问题
- 浅析iterator与指针的区别
- 探讨C++中数组名与指针的用法比较分析
- C++编程指向成员的指针以及this指针的基本使用指南
- 详解C++中的指针、数组指针与函数指针
- C++中字符串以及数组和指针的互相使用讲解
- C语言安全之数组长度与指针实例解析
- C++中指向对象的常指针与指向常对象的指针详解
- 指向变量的常指针与指向常变量的指针详细解析
- C#通过指针实现快速拷贝的方法
- php中将指针移动到数据集初始位置的实现代码[mysql_data_seek]
- C#通过指针读取文件的方法
- C语言指针学习经验总结浅谈
- C++交换指针实例
- C/C++中指针和引用之相关问题深入研究