问题
In one of the C++ articles on STL, is being said that -
Since container adapters do not support iterators, hence they cannot be used with the STL algorithms.
But it did not explain as to why Container Adapters do not support iterators? Can anybody explain me the same?
回答1:
What's the point of a stack or a queue having an iterator? A stack is by definition something that you can only push and pop into... An iterator would destroy the whole purpose of these adapters
回答2:
I would note that this is only an observation, not a rule.
That is, the Container Adapters provided in the STL do not support iteration, because they restrict the interface to conform to a specific model:
- A stack may only be manipulated at one end
- In a queue you may only push at one end and retrieve from the other
However, this is not a rule, and you may decide to create adapters that will support iteration.
来源:https://stackoverflow.com/questions/6569412/container-adapters-do-not-support-iterators