2

I am learning c++ and I have a function like so...

void int_swap(int *n, int *m){
  int temp; 
  temp = *n; 
  *n = *m; 
  *m = temp; 
}

I just want it to swap 2 ints. this is purely a learning exercise.

It works fine. I am just wondering of there is a better way to do this for objects of larger types. Can it be done without creating a temp?

user3738926
  • 1,178
  • 1
  • 10
  • 17
  • POD types or complex types? – Beta Oct 07 '15 at 04:15
  • The better way is a temporary and move semantics for general types (which is what `std::swap` does) and specialized versions of it where needed. – chris Oct 07 '15 at 04:15
  • 1
    Swapping two ints can be done without using a temp using xor, in your example it would be done with *n = *n ^ *m; *m = *n ^ *m; *n = *n ^ *m; – Chris Taylor Oct 07 '15 at 04:16
  • @ChrisTaylor, Even then, `int_swap(pointerTo5, pointerToOther5);` – chris Oct 07 '15 at 04:16
  • @Beta Primitives, but I guess I would be interested in objects too – user3738926 Oct 07 '15 at 04:19
  • @Jonathan This is a learning exercise and prefer to do without the libraries. Thanks you though – user3738926 Oct 07 '15 at 04:20
  • Which do you want? Better? Or without creating a temp? Because those are conflicting requirements. – Benjamin Lindley Oct 07 '15 at 04:22
  • @ChrisTaylor [Reasons for avoidance (of XOR swap) in practice](https://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice) – user2864740 Oct 07 '15 at 04:24
  • @Chris Taylor This is just what I was looking for (solution with xor). – user3738926 Oct 07 '15 at 04:24
  • I suggest you master primitives first, then try structs. – Beta Oct 07 '15 at 04:24
  • Possible duplicate of [Using pointers to swap int array values](http://stackoverflow.com/questions/1670821/using-pointers-to-swap-int-array-values) –  Oct 07 '15 at 04:26
  • @Benjamin, looking for it to be scale-able – user3738926 Oct 07 '15 at 04:26
  • @user3738926 So, inventing a solution for a problem that doesn't exist, and possibly creating more problems in the process.. gotcha. – user2864740 Oct 07 '15 at 04:28
  • @user2864740, the question was not what the compiler does... it was how to swap without using a temp. There is tremendous value in learning these bit manipulation tricks, it helps you think about problems in different ways and often gain a better understanding of how things work at that level. – Chris Taylor Oct 07 '15 at 04:32
  • @ChrisTaylor Sure, learn it. Then learn there is no reason to use it (if there is a case these days it is in very specific scenarios, outside the general scope of this question). File it as a bit of interesting trivia and then realize it is not a "better way" and not "[more] scale-able". I'm countering the "can be done" with reasons not to do this, even after knowing it can be done. – user2864740 Oct 07 '15 at 04:33
  • Once upon a time you could improve this by using `register int temp` to remove the temporary from memory. Nowadays, this is what the compilers do all by themselves. – Bo Persson Oct 07 '15 at 07:19

3 Answers3

3

XOR Swap does not need an intermediate variable:

https://en.wikipedia.org/wiki/XOR_swap_algorithm

From the Wiki page:

 void xorSwap (int *x, int *y) {
     if (x != y) {
         *x ^= *y;
         *y ^= *x;
         *x ^= *y;
     }
 }

Although most modern compilers automatically optimise your code so it is possible that your compiler has already chosen the most efficient method of swapping the two variables. See "Reasons for avoidance in practice" section in the wiki article.

ALM865
  • 1,078
  • 13
  • 21
  • So why *not* use an intermediate value? Why/how *would* an XOR swap benefit in this case? The temporary variable is the 'same amount of code', only one is trivially valid over more cases. – user2864740 Oct 07 '15 at 04:26
  • 2
    The OP asked " Can it be done without creating a temp? " – ALM865 Oct 07 '15 at 04:26
2
void swap(int &a,int &b)
{
    a=a+b;
    b=a-b;
    a=a-b;
}

You can use references

Himanshu
  • 4,327
  • 16
  • 31
  • 39
AkaSh
  • 486
  • 4
  • 16
0
template<typename T> 
void swap(T& x, T& y) 
{
    T temp = std::move(x);
    x = std::move(y);
    y = std::move(temp);
}

void swap(int & x , int& y) 
{
    if(x != y) 
        y ^= x ^= y ^= x;  
}

void swap (int& x, int& y)
{
    x += y;
    y = x - y;
    x -= y;
}
MAG
  • 454
  • 5
  • 14
  • The xor version might have problems with updating the variables more than once between sequence points. The add and sub might cause an overflow. – Bo Persson Oct 07 '15 at 07:05