Hey there!
I think everyone know what does this piece of code do:
vector <int> v = {1, 2, 3, 4};
for (int x : v)
cout << x << " ";
prints: 1 2 3 4
Is there any way to do this in reverse order ?!
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Hey there!
I think everyone know what does this piece of code do:
vector <int> v = {1, 2, 3, 4};
for (int x : v)
cout << x << " ";
prints: 1 2 3 4
Is there any way to do this in reverse order ?!
Name |
---|
http://ideone.com/AS8l6J
to Bassel and MrDindows
The main point of this C++ 11 feature is not to write long code, even the traditional way is better than those options
What advantages does the method using a struct and template have over the simple method kingofnumbers provided?
I don't think they have any advantages, they are long code and even slower.
however, Kemal asked for even shorter method using C++ 11, I don't know it,though. I was just saying that even the way I wrote is better than those struct and template ways
*
int(v.size()) - 1
:)I got TLE once :) thanks for reminding me :)
That's a great way to illustrate the usefulness of reverse adapter. The bug you just made couldn't be possible in the original code. (Not to mention that the original one is shorter, clearer and works with any container.)
can you tell which bug are you talking about? I've just tested my code and it's working even if the vector is empty.
and I will repeat my point, I think that Kemal actually requested something short and practical to use during the contest. for me I don't prefer to copy/paste that template every time I want to traverse a container in reversed order.
Yeah, you're right, my mistake :) The other points still stand though:
Casting UINT_MAX from unsigned to signed is implementation-defined, so it could possibly be a bug in some machine / compiler. For programming contests this is a moot point because you know the environment you're dealing with.
but in this case it isn't casting -1, it is unsigned zero subtract 1, and it equals to max value of unsigned(4294967295). Then it casts to int and produces undefined (not unspecified) behavior
One problem here that it won't work with temporaries, e.g
Please, don't invent bicycle. It's already in Boost.
Such simple!
Array [1 2 3 4] will be printed in reverse order.
There was a post: http://mirror.codeforces.com/blog/entry/10124 where using templates you can write in C++ like this:
Using C++20:
Any effect on time complete?
I did a few experiments running a loop for $$$10^9$$$ iterations, looks like performance depends on compiler optimizations. If no optimizations used at all, then ranges perform quite slow, however if you turn on some optimizations, then the difference with regular loop becomes really insignificant.
No optimizations (-O0):
Using -O2 optimizations:
Using -O3 optimizations: