问题
What is the purpose of the sequential container adaptors (i.e; stack, queue) in C++?
Thanks.
回答1:
They provide a narrower interface that enforces additional invariants, and are therefore safer to use when you want those invariants to be kept.
回答2:
- they keep you from doing things that you decided to be unlegal (eg. if the order of processing elements is important you can use a stack for the proper order)
- they point out the proper usage of a container to the user of your code (eg. prevent the user from accessing data he shouldn't be)
- they allow to implement the same structure using different underlying types (eg. depending on your exact problem you may be better off implementing a stack on top of a deque or on top of a vector)
回答3:
The best answer to your question would be to read the following book:
Effective STL
However if you want a quick and dirty answer: Not only do stacks and queues model real world objects such as program stacks and process queues, they also are optimal for random insert and delete operations.
来源:https://stackoverflow.com/questions/4753690/sequential-container-adaptors-in-c