0

What is the most elegant way (less code?) of reversing a stack in increasing order in an alternating manner? (non recursively)

EX.

1 2 3 4 5 6 7 8 9 10

1 [3 2] 4 5 6 [10 9 8 7]
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
herpderp
  • 33
  • 1
  • 5

2 Answers2

1

I would use std::reverse. Will this work for you?

http://www.cplusplus.com/reference/algorithm/reverse/

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
  • This is the answer, although I think this doc is better: http://en.cppreference.com/w/cpp/algorithm/reverse – 111111 Mar 01 '13 at 19:54
  • Both references seem about the same to me. – StilesCrisis Mar 01 '13 at 19:56
  • one is community editted on is a single authors take on it. cplusplus.com has a history of poor doc. – 111111 Mar 01 '13 at 19:58
  • 1
    I'm certainly not saying EVERY page on cplusplus.com is gold, I just feel like the one they have for `std::reverse` is more than serviceable. – StilesCrisis Mar 01 '13 at 19:59
  • `std::stack` doesn't have iterator so how can you use `std::reverse`? – Caesar Mar 01 '13 at 21:31
  • The OP didn't say `std::stack`. If that's what he meant, I misunderstood. I just assumed he had a container of values. – StilesCrisis Mar 02 '13 at 00:23
  • @StilesCrisis What is the most elegant way (less code?) of reversing a **stack** in increasing order in an alternating manner? (non recursively) – Caesar Mar 02 '13 at 05:15
  • Sure, I get it. I typically implement a stack as a `std::vector`. If he meant `std::stack`, then he's got your answer, right? – StilesCrisis Mar 02 '13 at 06:04
1

std::stack is designed to be a LIFO (last-in first-out), and so it was not designed for you to change the indexes of values. If you must change the index of the items than I would recommend using a different list.

enter image description here (Does anyone know who the original creator to this image so I can give proper citation)

Caesar
  • 9,483
  • 8
  • 40
  • 66