简体   繁体   中英

std::vector vs std::stack

What is the difference between std::vector and std::stack ?

Obviously vectors can delete items within the collection (albeit much slower than list) whereas the stack is built to be a LIFO-only collection.

However, are stacks faster for end-item manipulation? Is it a linked list or dynamically re-allocated array?

I can't find much information about stacks, but if I'm picturing them correctly (they are similar to an actual thread stack; push, pop, etc. - along with that top() method) then they seem perfect for window-stacking management.

A stack is not a container; it is a container adapter . It has a vector , deque or similar container that it stores as a member that actually holds the elements. Remember: it is declared as:

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

All stack does is limit the user interface to this internal container. The performance characteristics of the operations are exactly whatever the underlying container's performance characteristics are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM