4

I'm looking for standard container (if any exists) that will have constant time for:

  • access any element by position
  • pop element from the front
  • push element at back

I can program it by myself, but why bother if it could already exist in std?

kravemir
  • 10,636
  • 17
  • 64
  • 111
  • 1
    possible duplicate of [In which scenario do I use a particular STL Container?](http://stackoverflow.com/questions/471432/in-which-scenario-do-i-use-a-particular-stl-container) – Nicol Bolas May 20 '12 at 11:47
  • I haven't seen it before, but it's abstract question compared to mine. – kravemir May 20 '12 at 11:50

2 Answers2

8

std::deque is your friend. It's a double-ended queue with random access to elements.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
4

You can use a std::deque. It satisfy's all your requirements.

  • access any element by position

It provides random access using random iterators, as well as operator []

  • pop element from the front

It provides pop_front()

  • push element at back

It provides push_back()

Alok Save
  • 202,538
  • 53
  • 430
  • 533