Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ How do you swap variables with C++?

Malcolm

Administrator
Administrator
Staff Team
Code Plus
Company Plus
Hello Coders!

I'm looking for help swapping variables with C++, I know that you can use a temporary variable but you can also do it without it. Can someone show me both ways?

[CODE lang="cpp" title="C++"]int a = 5;
int b = 10;

//I want A to 10 and B to be 5.
// I think I have an idea, is this correct?

a=a+b;
b=b-a[/CODE]
 
Last edited:
Hello Coders!

I think I have finally figured it out after doing some research and practice. There are two ways of swapping variables in C++; one is using a temporary variable and the other way is without using a temporary variable.

With temp variable:

[CODE lang="cpp" title="C++ With temp variable"] //Variables
int a = 2;
int b = 4;
int temp; //declaring temp variable with no value.

temp = a; // Setting the temp value of A. Temp is now 2.
a = b; // A equals the value of B (4). So A is now 4.
b = temp; // B equals the temp value, which was 2.[/CODE]

Without temp variable:

[CODE lang="cpp" title="C++ Without temp variable"]//Variables
int a = 2;
int b = 4;

a = a + b; // A (2) equals to A(2) + B(4) being added together which creates a total of 6.

b = a - b; // B (4) equals to A(6) being subtracted by B(4); So 6-4 which equals 2. B is now 2.

a = a - b; // A (6) equals to A(6) being subtracted by B's new value of (2) which 6-2 equals 4.

// A = 4.
// B = 2.[/CODE]
 
Last edited:
I always used the temporary variable method. I find pretty cool that you found a way to just juggle the values without creating a new one. I am sure this kind of gymnastics can be useful to be applied in other codes when adding a new variable might just complicate things.

I was going to say that the temporary method might be easier, but the one without temporary is the same amounts of lines, so it is actually better.
If I am not misremembering you can even short it at parts, since you are putting a value that is the same as the variable plus an operator. That happens so often the C language has a shortcut
C++:
a += b;       // Another way to say a = a + b 
b = a - b;    // I am rusty so I am not sure if there is a way to shift this one without complicating things.
a -= b;       // a = a - b
Anyway, what I mean is that with this, the alternative without the temporary valuable ends up being almost as short. Anyway, that's anew trick for me, so thanks for sharing the solution.
 
Apparently you can use std::swap too from the standard library. In general the temporary variable will probably be a better choice (which the standard lib uses), since it's A. a general solution that works beyond ints, B. can be optimized by the compiler to perform optimally on the target platform, and C. avoids issues with overflows and underflows if the sum of the integers exceeds max or min int.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom