It depends on the underlying container of the std::stack
:
template <class T, class Container = deque<T>> class stack;
The class template acts as a wrapper to the underlying container
By default, Container = deque<T>
. And std::deque
is not contiguous:
the elements of a deque are not stored contiguously
Therefore,
stack<int> s;
is not contiguous because std::deque
is not contiguous.
However,
typical implementations (of std::deque
) use a sequence of individually allocated fixed-size arrays
That is why some of the test cases failed; the contiguity broke when the stack grew more than the size of one of the underlying fixed-size arrays.
If the underlying container is specified explicitly (the standard containers std::vector
and std::list
satisfy the requirements apart from std::deque
) and if that container is contiguous, then that stack is also contiguous.
For example,
stack<int, vector<int>> s;
is contiguous because std::vector
is contiguous.
TLDR
The contiguity of std::stack
is determined by the contiguity of its underlying container.
I would also like to thank the community for showing me ways of how people find answers to such programming questions and making me capable of digging solutions from references.