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 | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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: