开发者

Sequential container adaptors in C++

开发者 https://www.devze.com 2023-02-06 09:24 出处:网络
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 s

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.


  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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消