简体   繁体   English

STL容器适配器不一致

[英]Inconsistency in STL container adapters

STL queue has front() and back() methods, but stack has only top() , but not bottom() . STL 队列front()back()方法,但堆栈只有top() ,但不是bottom() Why is that so? 为什么会这样?

The only reason for stack is to prohibit access to the wrapped container except as a stack, ie accessing the top only. stack的唯一原因是禁止访问包装容器,除非作为堆栈,即仅访问顶部。 If you want a container that can be used as a stack but which is not restricted to only working as a stack, then you can use a different sequence container, such as a raw vector . 如果您想要一个可以用作堆栈但不限于仅作为堆栈工作的容器,那么您可以使用不同的序列容器,例如原始vector

Similarly, the only reason for queue is to prohibit access to the wrapped container except as a queue, ie accessing the front and back. 类似地, queue的唯一原因是禁止访问被包装的容器,除了作为队列,即访问前面和后面。 If you want a container that can be used as a queue but which is not restricted to only working as a queue, then you can use a different sequence container, such as a raw vector . 如果您想要一个可以用作队列但不仅限于作为队列工作的容器,那么您可以使用不同的序列容器,例如原始vector

A queue typically represents a first in first out (FIFO) buffer. 队列通常表示先进先出(FIFO)缓冲区。 You can access both ends. 您可以访问两端。 One end has the most recent item and the other end has the oldest item. 一端有最新的项目,另一端有最旧的项目。 You might want to access the most recent item from the code that is adding elements to the queue and the oldest item from code that processes items from the queue. 您可能希望从代码中访问最新项目,该代码将队列中的元素添加到队列中,而最旧项目则来自处理队列中项目的代码。

A stack typically represents a last in first out (LIFO) buffer. 堆栈通常表示后进先出(LIFO)缓冲区。 You can only access one end, which is the most recent item added to the stack. 您只能访问一端,这是添加到堆栈中的最新项目。 So there only needs to be a top(), and no bottom(). 所以只需要一个top(),而不是bottom()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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