Sequential container adaptors in C++

爱⌒轻易说出口 提交于 2019-12-11 08:48:49

问题


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:


  1. 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)
  2. 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)
  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!