7

Possible Duplicate:
C++11 reverse range-based for-loop

Is there an inverse range-based for in C++11?

I want to do something like this:

for(int value : vec)
{
    cout << value << endl;
}

To do this:

for(auto it = vec.rbegin(); it != vec.rend(); ++it)
{
    cout << *it << endl;
}

For instance:

for(int value : -vec)
{
    cout << value << endl;
}    

Is possible do something like that to do an inverse loop?

Community
  • 1
  • 1
Lucas Lima
  • 1,465
  • 2
  • 16
  • 28
  • 4
    I believe most STL containers also provide reverse iterators do they not? (for those of us in large projects where Boost is not an option) – im so confused Sep 10 '12 at 22:40
  • 2
    What did "Is there a C++11 solution" mean? If you meant "Is there a solution without Boost?", then the answer, ***as always***, is yes: implement the same thing as Boost. – R. Martinho Fernandes Sep 10 '12 at 23:09

1 Answers1

12

You can just use Boost.Range's reversed adaptor:

for(int value : ( vec | boost::adaptors::reversed ))
{...}

But standard C++11 doesn't have a similar feature.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 4
    Why do C++ people insist on such weird syntax? Is `reversed(vec)` so bad?? – Brendan Long Sep 10 '12 at 22:40
  • 14
    @BrendanLong FWIW, `boost::adaptors::reverse(vec)` (the "missing" `d` is intended) works too. But using `|` allows chaining several adaptors without getting LISPy. – R. Martinho Fernandes Sep 10 '12 at 22:42
  • As the robot says, it's much simpler to do that if you want to put multiple of them together. – Puppy Sep 10 '12 at 22:44
  • @Brendan: There's nothing weird about that syntax. It's no more weird than using `<<` to write things to streams. It simply takes getting used to. – Nicol Bolas Sep 10 '12 at 22:51
  • 3
    @BrendanLong, why do people ask questions that are explicitly covered by [rationale in the documentation](http://www.boost.org/doc/libs/1_51_0/libs/range/doc/html/range/reference/adaptors/introduction.html)? Is RTFM so hard?? Also, have you never used unix pipes? If you have, the syntax isn't so weird, if you haven't, [here's a nickel, kid](http://dilbert.com/strips/comic/1995-06-24/) – Jonathan Wakely Sep 10 '12 at 23:40
  • 2
    @NicolBolas `<<` is exactly what I meant by C++ people using weird syntax ;) Q: How do you print something in C++? A: Bit-shift `cout` over by the value of the string. – Brendan Long Sep 10 '12 at 23:44
  • @BrendanLong, `<<` is arguably one of the worst examples of operator overloading ... once you're used to it it makes sense, but otherwise it's a real WTF. I think `|` for range adaptors makes a lot more sense. – Jonathan Wakely Sep 10 '12 at 23:45
  • 2
    @JonathanWakely I've used pipes in bash, not in C++.. and I doubt many people would complain that C++ "isn't enough like bash".. – Brendan Long Sep 10 '12 at 23:46
  • 1
    @BrendanLong, I'm one of those people ;) I wanted it to be easier to compose C++ programs from existing tools instead of having to reimplement them, exactly as one does in the unix shell, so I wrote [the code to make it work](http://pstreams.sf.net/) ... which allows you to combine `<<` with `|` .. right up your street ;) – Jonathan Wakely Sep 10 '12 at 23:49