Skip to content

Instantly share code, notes, and snippets.

@aaronblohowiak
Created March 9, 2012 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronblohowiak/2008686 to your computer and use it in GitHub Desktop.
Save aaronblohowiak/2008686 to your computer and use it in GitHub Desktop.
swap in c++
/ 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