Learn C++ STL

Lesson 2 of 9 · stack and queue: Last In First Out, First In First Out

Module 6 · stack and queue: Last In First Out, First In First Out

Every stack and queue Operation, One by One, With Its Cost

Free with an accountReading

Bob keeps his undo history in a stack<int> of change numbers, and today he wants to print it. He writes the loop that worked on every vector and deque: for (int x : s). GCC 12 refuses with error: no matching function for call to 'begin(std::stack<int>&)'. A stack has no way to walk through its elements, and that is on purpose. This lesson lists every call the two adaptors do have, each with its cost, and then everything they refuse, Bob's loop first.

Checking what this lesson needs for you…

What is inside Every stack and queue Operation, One by One, With Its Cost

  1. stack: making one
  2. stack: push and emplace
  3. stack: top
  4. stack: pop
  5. stack: size and empty
  6. stack: swap
  7. stack: == and <
  8. queue: making one
  9. queue: push and emplace
  10. queue: front and back
  11. queue: pop
  12. queue: size, empty, swap, == and <
  13. What neither adaptor has
  14. Emptying one: three ways
  15. Printing one: pop a copy
  16. Every cost in one table, and the measurement