#Variable swap code
64 messages · Page 1 of 1 (latest)
When your question is answered use !solved or the button below to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Will there are multiple things wrong with... All of that
First, == is equality, not assignment.
Second the comma operator doesn't make tuples, it just evaluates both side and "returns" the second.
Third, even if you had a pair, you can't do a decomposing assignment to already existing variables
All in all, this isn't python
If you want to swap, either do it manually using a temporary variable, or use the std::swap method
okay thank you
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
So, did you manage to implement a swap (without using std::swap)?
If yes, as a small challenge you could try to swap the two elements without using a 3rd temporary variable (and without using std::swap of course)
std::exchange 

#include <iostream>
int main() {
double filesize = 100;
double sales = 10.10;
double emptybucket = filesize;
filesize = sales;
sales = emptybucket;
std::cout << (filesize);
return 0;
}
||
a = a + b;
b = a - b;
a = a - b;
||
wow
does swapping differ from language to language? Are we supposed to remember different logics for different languages?
nope (although there are some small syntactical improvements in other languages)
it all works the same
okay
ah, algebra~y trick
amaze amaze amaze
you could actually do something horrible with tuples and tuples of references like:
std::tie(b,a) = std::tuple(a,b);
but that does needless copies and is just, bleh
Yeah, with floating point numbers it's a bit iffy because of rounding errors, but that's fine given the beginner context
!cppref std::tie
template<class... Types>
std::tuple<Types&...> tie(
Types&... args) noexcept;
// ... and 1 more
(always forget what this does)
std::tie works the same like
auto [a, b] = ...;
right?
yesn't
awesome
auto [a,b] would copy
while tie(a,b) makes a tuple of references
also, std::tie would assign to the variable right
the = is the part assigning
tie is kind of doing the opposite
tying together two things into one tuple
while auto& [a,b] "takes apart (not really)" to parts of a tuple/struct/...
Can you do
auto &[a, b] = ...;
```or smth like:
```cpp
auto [&a, &b] = ...;
```or smth alike? Pretty sure you can't, but since you're here already, figured why not ask
i see
the first one, yes
oh wow, really? Damn
it doesn't actually split apart the object in C++, it keeps the whole thing around,
but binds the names a, b to the two members
so you can only decide whether the whole thing is a reference or copy
not the individual parts
i see
makes sense, pretty much what you would expect
sad
you'd have to do
auto [a, x] = ...;
auto [b, c] = x;
😛
not cursed enough
i did not undertand a single thing
just do this:
temp = a;
a = b;
b = temp;
and don't concern your self with other things [ for now ].
how does this things logic work?
a=5
b=2
a=7
b=3
a= 3?
Try it out and see