There is a difference between the theory of a stack in generic programming and
std::stack. Which is what I (tried) to point out. I never said
std::vector couldn't be used for a variety of tasks.
This discussion is geared towards basic elementary understanding of
"what is a std::vector?".
I could very easily have blathered on a lot more about
std::vector and its uses. And misuses.
The memory allocation for elements of a std::vector according to the standard is contiguous, the same as a C-style vector. Other containers have their elements scattered around in memory, they are not contiguous.
Yes, using a
std::vector as the base class for the adapter for a custom designed stack is entirely doable. If'n I ever needed to "roll my own" stack solution. But are other C++ containers better suited to the task? Yes, there are.
Right or wrong, I'm of the opinion if C++ provides something, such as
std::stack, I am not about to manually mash something together.
Unless I have very specific needs the C++ stdlib
std::stack doesn't provide.
Even if earlier C++ versions required creating my own stuff. I might as well just code in machine language then.
Each C++ stdlib container has its uses, and deficits. Memory layout, random access of elements, speed of adding and removing elements, etc. There is no one container that fits every need. Hence the decision by the standards committee to create a variety of containers.
I myself use
std::vector almost exclusively, even in multi-dimensional usages. 2nd most used, around 2-3%, is
std::list. With what I do I don't need sets or maps or stacks or the other containers. Maybe one day.
C++26 will add a new container:
std::inplace_vector.
https://en.cppreference.com/w/cpp/container/inplace_vector
I can see some definite advantages to having a specified number of max elements. Though the syntax for construction is as clunky as for a
std::array IMO.
Crufting a multi-dimensional
std::array is a bit perverse, setting each dimension is in reverse order of what to expect from a C-style MD array or a MD
std::vector. I'd bet the inplace_vector has the same or similar problems.
I haven't yet really looked at C++23
std::mdspan beyond knowing it exists. There are very few examples of usage available on the
interwebz. The one example I could find makes using it rather complicated with extents, etc.
https://www.studyplan.dev/pro-cpp/mdspan