Questions tagged [stdstack]

The std::stack class is a container adapter in C++ that gives the programmer the functionality of a stack - specifically, a LIFO (last-in, first-out) data structure.

std::stack is defined in the Standard Library header <stack>:

template<class T, class Container = std::deque<T>> class stack;

The class template acts as a wrapper to the underlying container - only a specific set of functions is provided. The stack pushes and pops the element from the back of the underlying container, known as the top of the stack.

Reference: https://en.cppreference.com/w/cpp/container/stack

11 questions
4
votes
2 answers

Is std::stack contiguous?

I wanted to find the maximum element in a stack, and thought of using std::max_element. Then I came to know that std::stack does not have begin() and end() functions. After surfing the net, I saw a hack: stack s({0, 1, 2, 3, 4, 5, 6, 7, 8,…
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
3
votes
1 answer

std::stack implementation on different containers what is the actual difference?

when implementing an std::stack there are a few options, for example: // stack with default underlying deque std::stack< int > intDequeStack; // stack with underlying vector std::stack< int, std::vector< int > > intVectorStack; …
Alex Butane
  • 127
  • 8
2
votes
2 answers

Why do std::flat_set and std::flat_map have overloaded constructors for std::initializer_list while other container adapters don't?

I noticed that C++23 added new overloads for the std::stack and std::queue container adapters' constructors, that allow to construct the underlying container with the contents of the range [first, last). cppreference also shows how these overloads…
LoS
  • 448
  • 1
  • 3
  • 15
1
vote
0 answers

How to brace initialize a std::stack in C++?

I have a struct, one of whose members is a std::stack using a std::array as its underlying container: struct S { int a, b; // dummy members std::stack< int, std::array > st; }; I would like to use brace initialization to…
nluka
  • 91
  • 6
1
vote
3 answers

Did not understand C++ stack behavior

I got stuck for two hours trying to understand what is happening in this simple C++ test program, but still did not get it. It should just receive three strings as inputs, insert them into a stack and finally print all elements of this same…
1
vote
3 answers

Check if a stack is palindrome

In a recent coding interview I was asked to solve a problem in which the task was to complete a function which receives a stack as an argument by reference and checks if the passed stack is palindrome or not. I did come up with an approach but it's…
Arun Suryan
  • 1,615
  • 1
  • 9
  • 27
1
vote
1 answer

C++ Debug Assertion Failed; Expression: list iterators incompatible

I am fairly new to C++ and I am trying to make sense out of these exhausting error messages so far. I really got stuck in this one and it really makes no sense at all. The code that I shared below is a part of a personal directed graph header file I…
0
votes
0 answers

How to make a C++ STACK container iterable?

Can we write a myStack class that will be implemented in terms of std::stack so that it offers all of its member functions plus an additional feature: iterators? I tried some options, but they seem to me not quite optimal.
0
votes
0 answers

Is it safe to access the popped binary tree nodes which iterative traversed by std::stack

Considering the Solution of Leetcode 94. Binary Tree Inorder Traversal. What confusing me is whether is is safe to access the node after the node being popped(stk.pop(); res.push_back(root->val);). I know that the underline container of std::stack…
amont
  • 81
  • 8
0
votes
3 answers

Why am I unable to add elements to this stack?

The following code is giving me a runtime error for some reason I'm unable to figure out. We are iterating over the string a and whenever we encounter a char = (, we push 1 into the stack and whenever we encounter ) we remove an element from the…
K Kimash
  • 87
  • 1
  • 2
  • 9