#Variable swap code

64 messages · Page 1 of 1 (latest)

ocean crag
#

c++ allows mutability right then why isnt this working

#include <iostream>

int main() {
    double filesize = 100;
    double sales = 10.10;
    filesize, sales == sales, filesize  ;
    std::cout << (filesize); << (sales);
    return 0;
}
young glenBOT
#

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.

ocean tree
#

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

ocean crag
#

okay thank you

young glenBOT
# young glen

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

robust nacelle
# ocean crag okay thank you

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)

ocean crag
#

ah alr

#

the tutorial showed me the third variable way

robust nacelle
bronze sinew
#

i only know the xor way

#

are there any other way?

ocean crag
#
#include <iostream>

int main() {
    double filesize = 100;
    double sales = 10.10;
    double emptybucket = filesize;
    filesize = sales;
    sales = emptybucket;
    std::cout << (filesize);
    return 0;
}
robust nacelle
bronze sinew
#

wow

ocean crag
#

does swapping differ from language to language? Are we supposed to remember different logics for different languages?

robust nacelle
ocean crag
#

okay

bronze sinew
#

amaze amaze amaze

cold frigate
#

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

robust nacelle
#

!cppref std::tie

young glenBOT
robust nacelle
#

(always forget what this does)

bronze sinew
#

std::tie works the same like

auto [a, b] = ...;

right?

cold frigate
#

yesn't

bronze sinew
#

awesome

cold frigate
#

auto [a,b] would copy

bronze sinew
#

well

#

yeah

cold frigate
#

while tie(a,b) makes a tuple of references

bronze sinew
#

also, std::tie would assign to the variable right

cold frigate
#

the = is the part assigning

bronze sinew
#

heh

#

ok

cold frigate
#

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/...

robust nacelle
# cold frigate `auto [a,b]` would copy

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
bronze sinew
#

i see

robust nacelle
#

oh wow, really? Damn

cold frigate
#

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

bronze sinew
#

i see

robust nacelle
#

makes sense, pretty much what you would expect

bronze sinew
#

can i do a bind inside another one?

#

like
[a, [b, c]]

cold frigate
#

nope

#

(AFAIK)

bronze sinew
#

sad

cold frigate
#

you'd have to do

auto [a, x] = ...;
auto [b, c] = x;

😛

bronze sinew
#

not cursed enough

ocean crag
#

i did not undertand a single thing

pearl pebble
ocean crag
#

Okay thank you

#

why cant i put a=b; at the last

ocean crag
robust nacelle