Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Container Adapters do not support iterators

Tags:

c++

stl

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?

like image 229
XMarshall Avatar asked Jul 04 '11 09:07

XMarshall


People also ask

Which is not a container adapter?

The typical characteristic of a container adapter is that it is not a first-class, container-like sequence and associative containers, which means that it does not provide actual data structure implementation where elements are stored simply for storage and retrieval. It does not support iterators.

What are container adapters?

The container adapters are classes that provide a subset of a container's functionality but may provide additional functionality that makes it easier to use containers for certain scenarios.

Why is stack called a container adapter?

Container adaptors provide a different interface for sequential containers. stack: Adapts a container to provide stack (LIFO data structure) (class template). queue: Adapts a container to provide queue (FIFO data structure) (class template). priority_queue: Adapts a container to provide priority queue (class template).

How many interfaces does container adapter provide?

This is called a container adaptor. There are three container adaptors provided by C++: std::stack, std::queue, and std::priority_queue.


2 Answers

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

like image 154
Armen Tsirunyan Avatar answered Oct 21 '22 16:10

Armen Tsirunyan


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.

like image 39
Matthieu M. Avatar answered Oct 21 '22 14:10

Matthieu M.