What is the purpose of the sequential container adaptors (i.e; stack, queue) in C++?
Tha开发者_如何学JAVAnks.
They provide a narrower interface that enforces additional invariants, and are therefore safer to use when you want those invariants to be kept.
- 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)
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.
精彩评论