<st*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I knew some ways of swapping two ints without using a temporary
variable. Just like:
// Method 1:
void swap1(int &a, int &b) {
int temp = a;
a = b;
b = a;
}
I believe this is a mistake. I think you actually meant:
void swap1(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
because:
a = b;
b = a;
makes both those variables the same no matter what came before them. Think
about it. Say a = 5, b = 10.
a = b; a is now 10, b is still 10.
b = a; b is still 10, a is still 10.