What's the rationale for the push_back method name in C++ std::vector? For instance, is there a stack-based origin (push being a common stack operation)? Was there a pre-existing library that used these terms for adding to a sequence?
Besides common terms other APIs use like append and add, insert_end would seem to be more internally self-consistent (though front and back do exist elsewhere).
As you mention, push and pop are common names for stack operations. The reason it's not just push and pop is so that it can be consistent with the other containers. std::vector only implements push_back and pop_back, but there is also push_front and pop_front in, for example, std::list. Having consistent names is useful when writing generic functions.
I would assume that it's due to those methods facilitating the use of std::vector as a stack---if all you do is push_front(foo) and pop_front() (or the back equivalent), you've got a stack.
Related
Is there any standard way to have the equivalent of std::vector:reserve in a std::stack<T, std::vector<T>>?
Also, is there there a standard way to implement something like pop(int count) and have it destruct elements in the correct order?
If there is no such way, short of writing a custom stack implementation, that would also answer my question.
std::stack can be used with any sequence container, and std::vector is such a container.
Then you can pass a reference to an existing container (like a pre-allocated vector) to the std::stack constructor.
I have a c++ stack named pages.
As I have no clear() function to clear a stack, I wrote the following code:
stack<string> pages;
//here is some operation
//now clearing the stack
while(!pages.empty())
pages.pop();
Now my question: is there a better efficient way to clear the stack?
In general you can't clear copying containers in O(1) because you need to destroy the copies. It's conceivable that a templated copying container could have a partial specialization that cleared in O(1) time that was triggered by a trait indicating the type of contained objects had a trivial destructor.
If you want to avoid loop.
pages=stack<std::string>();
or
stack<std::string>().swap(pages);
I don't think there is a more efficient way. A stack is a well defined data type, specifically designed to operate in a LIFO context, and not meant to be emptied at once.
For this you could use vector or deque (or list), which are basically the underlying containers; a stack is in fact a container adaptor. Please see this C++ Reference for more information.
If you don't have a choice, and you have to use stack, then there is nothing wrong with the way you do it. Either way, the elements have to be destroyed if they were constructed, whether you assign a new empty stack or pop all elements out or whatever.
I suggest to use a vector instead; it has the operations you need indeed:
size (or resize)
empty
push_back
pop_back
back
clear
It is just more convenient, so you can use the clear method. Not sure if using vector is really more performant; the stack operations are basically the same.
What about assigning a new empty stack to it?
pages = stack<string>();
It won't remove elements one by one and it uses move assignment so it has the potential to be quite fast.
make it into a smart pointer:
stack.reset();
stack = make_shared<stack<string>>();
What about subclassing std::stack and implementing a simple clear() method like this, accessing underlying container c ?
public:
void clear() { c.clear(); }
I am writing a very simple std::stack using vector as its underlying container. I realized that I could replace all the push(), pop() and top() functions with push_back(), pop_back() and back() of the vector container.
My questions are: why to use a container adaptor when the controlled use of the underlying container is enough? Why not to use just a deque, vector or list? There will be waste of memory or processing time?
When your code says std::stack it's clear to the reader what operations they need on the container... it communicates and documents while enforcing that no other operations are used. It may help them quickly form an impression of the algorithmic logic in your code. It's then easy to substitute other implementations that honour the same interface.
It's a bit like using std::ifstream instead of std::fstream - you can read and write with std::fstream, but whomever reads your code will need to consider more possible uses you put the stream to before realising that it's only being used for reading; you'd be wasting their mental effort.
By reverse I mean swap the order of the elements so a stack containing ABCD ends up with DCBA. I am currently using vectors instead of stacks just because of the reverse operation.
std::stack<T> operates by wrapping an underlying container, typically std::deque<T>.
// According to cppreference.com
template<
class T,
class Container = std::deque<T>
> class stack;
There's no reason to use a std::stack instead of a std::vector or a std::deque unless all you care about is the top element. The structure doesn't even expose iterators, and that's part of the design.
Sure you can write a fancy function to reverse it (store the top, pop, push to a new stack, repeat), but why do more work than you have to?
A stack is a container that provides a nice simple push/pop/top interface. What you are asking for is actually not stack functionality so that implies that a stack is the wrong container for your needs.
Most likely either a vector or deque would be a good choice (as you have already mentioned in your question) as they provide constant time push/pop back capabilities, as well as a linear time reversal (std::reverse).
The STL stack is not a container, it's a container adapter ( http://www.cplusplus.com/reference/stack/stack/ ). The underlying container type is passed as an argument to the template, with the default being deque. The short answer to your question is 'no' -- the stack interface doesn't provide that capability.
The two options I see are:
Use the stack interface (pure stack solution) as described above (link repeated here for convenience):
http://dev-faqs.blogspot.com/2012/02/reverse-given-stack-in-place.html
Use a more flexible structure as you have done (e.g. vector or deque). In this case it probably makes the most sense to use a deque and push/pop from either end, depending on whether you're using the sequence in "forward" or "reverse" mode. That's most efficient.
I am learning data structures and algorithms. My teacher told me the following:
push(): push inserts an element in the stack
pop(): pop deletes the last inserted element from the stack
size(): Returns the number of elements in the stack
isempty(): Returns a boolean indicating if the stack is empty
top(): returns the top element of the stack, without removing it; If stack is empty an error is returned.
Where are the implementations of these functions? Are these built-in functions of C++?
Your teacher was explaining the functions available for the generic stack data structure. Those functions aren't implemented anywhere in particular because your teacher was not talking about any specific stack. Your teacher wasn't even talking about C++. Right now you're just learning about what a stack is: they're abstract data structures implementable in any language. They're last-in-first-out containers. Later in your course, you'll learn about trees, queues, heaps, lists, and all sorts of other abstract data structures. In a later lesson, your teacher will probably demonstrate how to implement the functions listed above. The demonstration might even be in C++.
These are member functions for std::stack, which is a container class (template) in the C++ standard library.
You can find the implementation in the <stack> header file.
[Note that it's empty(), not isempty(), though.]
std::stack - it's in the standard library, which is "part" of c++, although it's not a "built-in function"
If you are trying to figure out how to create your own template classes and functions, see
http://www.cplusplus.com/doc/tutorial/templates/
If you want to understand how a stack could be implemented, and what a stack is, read
http://en.wikipedia.org/wiki/Stack_%28data_structure%29
If you want to understand more about containers in C++ STL, read these:
http://www.cplusplus.com/reference/stl/stack/
http://www.cplusplus.com/reference/stl/
Hopefully, these references will help you with asking a more specific question or to request clarification on something you don't yet understand...
To use the stl you would have to include all or part of it in your files, or reference the stl each time you use it.
Here is a link to an implementation of the STL:
http://www.sgi.com/tech/stl/download.html