Container (abstract data type)


In computer science, a container is a class or a data structure whose instances are collections of other objects. In other words, they store objects in an organized way that follows specific access rules.
The size of the container depends on the number of objects it contains. Underlying implementations of various container types may vary in size, complexity and type of language, but in many cases they provide flexibility in choosing the right implementation for any given scenario.
Container data structures are commonly used in many types of programming languages.

Function and properties

Containers can be characterized by the following three properties:access, that is the way of accessing the objects of the container. In the case of arrays, access is done with the array index. In the case of stacks, access is done according to the LIFO order and in the case of queues it is done according to the FIFO order;storage, that is the way of storing the objects of the container;traversal, that is the way of traversing the objects of the container.
Container classes are expected to implement CRUD-like methods to do the following:
  • create an empty container ;
  • insert objects into the container;
  • delete objects from the container;
  • delete all the objects in the container ;
  • access the objects in the container;
  • access the number of objects in the container.
Containers are sometimes implemented in conjunction with iterators.

Types

Containers may be classified as either single-value containers or associative containers.
Single-value containers store each object independently. Objects may be accessed directly, by a language loop construct or with an iterator.
An associative container uses an associative array, map, or dictionary, composed of key-value pairs, such that each key appears at most once in the container. The key is used to find the value, the object, if it is stored in the container. Associative containers are used in programming languages as class templates.
Container abstract data types include:
Common data structures used to implement these abstract types include:

Graphic containers

s also use containers, which are special widgets to group other widgets, such as windows, panels. Apart from their graphical properties, they have the same type of behavior as container classes, as they keep a list of their child widgets, and allow adding, removing, or retrieving widgets among their children.

In statically-typed languages

Container abstractions can be written in virtually any programming language, regardless of its type system. However, in strongly-typed object-oriented programming languages it may be somewhat complicated for a developer to write reusable homogeneous containers.
Because of differences in element types this results in a tedious process of writing and keeping a collection of containers for every elemental type.
Many elemental types are inherently incompatible with each other because of the memory size they occupy and their semantic meaning and therefore require different containers. Modern programming languages offer various approaches to help solve the problem: