While reading, it says that a vector's memory is continuous, while a deque's memory is not. So how then, I'm wondering, is random-access ( my_container[i] ) still possible in constant time with a deque?
Also, what's the point/advantage of using C++'s std::stack over directly using the individual Containers that are used to make an std::stack (deque by default, vector, or list), if any? (Note my question is not asking about whether to use a list vs a vector or whatever, just about the point of abstracting it into a stack).
Another related, but separate question:
I am trying to figure out the most efficient way to do the following code.
Using stack, I'd have to do something like this.
1 2 3 4 5
|
T a = numbers.top();
numbers.pop();
T b = numbers.top();
numbers.pop();
numbers.push(a * b);
|
This makes me have to make 2 temporary variables to store the top and second-to-top variables. (3 for the overloaded multiplication)
But I'm wondering if there is, logically, a better way to go about modifying my container like this? Is there a way to not have to create two temporary variables? I don't think there is logically no matter what container is used, but just wondering if any of you have an idea. =)
Vectors seem to take extra care to only reallocate when necessary, so should I most likely stick to a stack using the vector container for this instead of deque?
Edit: changed .read() to .top()