5
Swap
- #include <iostream>
- template <class T>
- void swap( T &p1, T &p2 )
- {
- T pt = p1;
- p1 = p2;
- p2 = pt;
- }
- int main()
- {
- using std::cout;
- using std::endl;
- int x = 2, y = 3;
- float a = 1.2, b = 5.7;
- const char *s = "John", *p = "Peter";
- cout << "x = " << x << ", y = " << y << endl;
- swap( x, y );
- cout << "x = " << x << ", y = " << y << endl;
- cout << "a = " << a << ", b = " << b << endl;
- swap( a, b );
- cout << "a = " << a << ", b = " << b << endl;
- cout << "s = " << s << ", p = " << p << endl;
- swap( s, p );
- cout << "s = " << s << ", p = " << p << endl;
- }
Comments