Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

std::stack

Explanation

LIFO (Last In, First Out). Uses std::deque internally.

See std::stack.

Time complexity:

OperationComplexity
push()O(1)
pop()O(1)
empty(), size()O(1)

Code

#include <print>
#include <stack>

int main() {
  std::stack<uint64_t> callStack;

  callStack.push(0x1000);
  callStack.push(0x2000);
  callStack.push(0x3000);

  while (!callStack.empty()) {
    std::println("Return to: {:#x}", callStack.top());
    callStack.pop();
  }

  return 0;
}

View on GitHub.

Output

$ ./src/data-structures/build/std-stack
Return to: 0x3000
Return to: 0x2000
Return to: 0x1000