Created
March 9, 2012 21:01
-
-
Save aaronblohowiak/2008686 to your computer and use it in GitHub Desktop.
swap in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ swap algorithm example | |
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
class CRectangle { | |
int x, y; | |
public: | |
void set_values (int,int); | |
int area () {return (x*y);} | |
}; | |
void CRectangle::set_values (int a, int b) { | |
x = a; | |
y = b; | |
} | |
void doTheSwap(CRectangle *x, CRectangle * y){ | |
CRectangle *a = x; | |
CRectangle *b = y; | |
swap(a, b); | |
} | |
int main () { | |
CRectangle small, large; | |
small.set_values(1,1); | |
large.set_values(10, 10); | |
CRectangle *smallptr, *largeptr; | |
smallptr = &small; | |
largeptr = &large; | |
cout << small.area(); | |
cout << " " << large.area(); | |
cout << endl; | |
swap(small, large); | |
cout << smallptr->area(); | |
cout << " " << largeptr->area(); | |
cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment