G
Guest
Can anybody tell me how I can put the arguments of an array in a different
order?
order?
Can anybody tell me how I can put the arguments of an array in a different
order?
John said:Can anybody tell me how I can put the arguments of an array in a
different order?
This code puts the elements (not arguments) of an array in a different
order, specifically it swaps the first two elements.
int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int t = a[0];
a[0] = a[1];
a[1] = t;
Does that help?
Rolf Magnus said:For int, you could of course also use the nifty xor trick:
a[0] ^= a[1];
a[0] ^= a[1];
John said:Can anybody tell me how I can put the arguments of an array in a
different order?
This code puts the elements (not arguments) of an array in a different
order, specifically it swaps the first two elements.
int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int t = a[0];
a[0] = a[1];
a[1] = t;
Does that help?
For int, you could of course also use the nifty xor trick:
a[0] ^= a[1];
a[0] ^= a[1];
And using the C++ standard library, you could use:
std::swap(a[0], a[1]);
which is to prefer because it is the solution that is best at showing
the intent.
Jack Klein said:For int, you could of course also use the nifty xor trick:
a[0] ^= a[1];
a[0] ^= a[1];
Except that for signed ints you run the risk of creating a trap value
and causing undefined behavior.
Jack Klein said:For int, you could of course also use the nifty xor trick:
a[0] ^= a[1];
a[0] ^= a[1];
Except that for signed ints you run the risk of creating a trap value
and causing undefined behavior.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.