How do I how to I swap variable values in C++? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

How do I how to I swap variable values in C++?

If I have int valueA = 8; int valueB = 11; How do I swap the values ? I am knew right coding and I’m stumped here.

13th Jan 2022, 3:59 AM
Tom Delawder
22 ответов
+ 14
What you should really do is simply this: int valueA = 8; int valueB = 11; int temp = valueA; valueA = valueB; valueB = temp; The other methods are fun but not really applicable most of the time. C++ also has a `swap` function but using it is probably not the point of the exercise :p
13th Jan 2022, 4:49 AM
Schindlabua
Schindlabua - avatar
+ 6
my favorite is the bitwise operator jaja https://code.sololearn.com/c26o4pSfqFJL/?ref=app
13th Jan 2022, 4:05 AM
CGM
CGM - avatar
+ 5
We can swap two numbers without using third variable. There are two common ways to swap two numbers without using third variable: By + and - By * and / // https://www.javatpoint.com/cpp-program-to-swap-two-numbers-without-third-variable https://www.programiz.com/cpp-programming/examples/swapping
13th Jan 2022, 4:04 AM
NEZ
NEZ - avatar
+ 4
Quoi Runtime the swap function is defined like template < class T > void swap(T& a, T& b); the function arguments are reference variables. If the term reference is new to you, check here: https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in
13th Jan 2022, 7:33 AM
Nikhil
Nikhil - avatar
14th Jan 2022, 9:38 AM
Schindlabua
Schindlabua - avatar
+ 3
Quoi Runtime A reference is actually a const pointer behind the scenes. C++ just hides the memory operators from you, for ease of use. Using "&" to mean two things—"reference-to" and "addres-of"—is pretty confusing. I'm not sure why they picked the same symbol..
13th Jan 2022, 8:36 AM
Schindlabua
Schindlabua - avatar
+ 3
Cristian Gabriel Mazzulla i got it thank you!
14th Jan 2022, 9:13 AM
Rishi
Rishi - avatar
+ 2
Rishi I can try, surely you know that computers work with bits, ones and zeros. The values ​​of each variable are stored in their binary representation, in this case we have 8 and 11, which in binary are .. 1000 and 1011 respectively If I'm not mistaken.. The XOR operator, or Exclusive OR, Compare two values ​​(in this case) and return true (or in this case 1) if 1 and only 1 of the inputs is true (odd true inputs in the case of 3 or more inputs) Applying this comparator Bit by Bit, We obtain that "a^=b^=a^=b" is equivalent to: (remember that a=1000 and b=1011 in binary) •First a=a^b: a=1000 ^ b=1011 _____ 0011 //So a=0011, Nothing for now •Then b=b^a: b=1011 ^ a=0011 _____ b=1000 //b is now 1000 (8 in decimal) •Finally a=a^b: a=0011 ^ b=1000 _____ a=1011 //Ready! a is 1011 (11 in base 10) I hope I was clear, now I am attaching a couple of links from here on SoloLearn .. https://www.sololearn.com/learn/4070/?ref=app https://www.sololearn.com/learn/4074/?ref=app
14th Jan 2022, 4:44 AM
CGM
CGM - avatar
+ 2
A=A+B B=A-B A=A-B
14th Jan 2022, 5:43 AM
Arun kumar
Arun kumar - avatar
+ 1
Schindlabua I am intrigued, in which case my beloved bitwise xor operator would not be applicable? I'm a bit of a noob yet haha
13th Jan 2022, 5:42 AM
CGM
CGM - avatar
+ 1
Schindlabua Oh wow, this also works: swap(valueA, valueB); But I ain't passing a reference to the swap function. Then how come the values get changed in the main's scope?
13th Jan 2022, 7:20 AM
Œ ㅤ
Œ ㅤ - avatar
+ 1
Schindlabua I see, that's cool
13th Jan 2022, 11:48 AM
Œ ㅤ
Œ ㅤ - avatar
+ 1
Cristian Gabriel Mazzulla sorry, I only saw that post just now! Xor swap works fine for numbers but for everything else it becomes iffy. Say you have a custom `Point` class and two objects that you want to swap, in that case xor will get you nowhere. (Either `Point` has no xor operator defined in which case the code won't compile, or it has a custom xor operator that might do anything. Not ideal either since the xor swap relies on the fact that you are doing a bitwise operation.) Though, at the end of the day, the main reason why I prefer the good old temp variable is because it's clear, and readable. `a^=b^=a^=b` is a cute brain-teaser and it's really cool that it works at all, but unless you have seen this exact sequence of symbols before it'll mean nothing to you and you have to spend 10 minutes figuring it out. And nobody wants that, code is meant to be read, and understood!
14th Jan 2022, 1:09 AM
Schindlabua
Schindlabua - avatar
+ 1
Schindlabua you are absolutely right, without getting to the case of the two custom objects, the bitwise xor operator doesn't even work with floats or doubles... And it is true that whoever has not seen it before will have to investigate what it means, that's how it was for me haha In the end, using a temp variable is the best. Thank you, I hope I get a chance to show off some of this in college ..😆
14th Jan 2022, 1:42 AM
CGM
CGM - avatar
0
Thank you all ! Schindlabua - I used your suggestion. Solved
13th Jan 2022, 5:06 AM
Tom Delawder
0
Nikhil That's pretty weird, why doesn't the reference variable need the mem loc to the variable instead of the variable? Like, why isn't it more like this: int &foo = &var; // rather than just var
13th Jan 2022, 7:36 AM
Œ ㅤ
Œ ㅤ - avatar
0
Manav Roy i couldnt get the variable swap to do anything in C++
13th Jan 2022, 4:26 PM
Tom Delawder
0
#include<iostream> usung namespace std; int main() { int a,b,c; cin>>a>>b; c=a+b; cout<<c; return 0; }
13th Jan 2022, 6:46 PM
Xurmatbek Olloyorov
0
Cristian Gabriel Mazzulla can you explain how the xor swap works plz? I couldn't get it😅
14th Jan 2022, 2:34 AM
Rishi
Rishi - avatar
0
How do my pic is slide in display in java
14th Jan 2022, 3:27 AM
Satyanarayan Majhi