Click here to Skip to main content
15,894,313 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The Question Title Says It All .. .
Posted

1 solution

In C++11 you can use the auto keyword to declare the temporary variable, for example:
C++
auto temp = a;
a = b;
b = temp;

That works for all types and classes that support the assignment operator.

For numeric types that support the exclusive-or operator you can use the following trick:
C++
a = a ^ b;
b = a ^ b;
a = a ^ b;

For an explanation see: http://en.wikipedia.org/wiki/XOR_swap_algorithm[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Apr-15 14:03pm    
5ed.
—SA
nv3 26-Apr-15 18:22pm    
Thank you!
Usman Hunjra 26-Apr-15 16:05pm    
Superb .. +5 Sir .. !! :)
nv3 26-Apr-15 18:22pm    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900