5

Swap

  1. #include <iostream>
  2.  
  3. template <class T>
  4. void swap( T &p1, T &p2 )
  5. {
  6.     T pt = p1;
  7.     p1 = p2;
  8.     p2 = pt;
  9. }
  10.  
  11. int main()
  12. {
  13.     using std::cout;
  14.     using std::endl;
  15.  
  16.     int x = 2, y = 3;
  17.     float a = 1.2, b = 5.7;
  18.     const char *s = "John", *p = "Peter";
  19.  
  20.     cout << "x = " << x << ", y = " << y << endl;
  21.     swap( x, y );
  22.     cout << "x = " << x << ", y = " << y << endl;
  23.     cout << "a = " << a << ", b = " << b << endl;
  24.     swap( a, b );
  25.     cout << "a = " << a << ", b = " << b << endl;
  26.     cout << "s = " << s << ", p = " << p << endl;
  27.     swap( s, p );
  28.     cout << "s = " << s << ", p = " << p << endl;
  29.  
  30. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].