Collection class
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includi ...
, a container is a
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
or a data structureEntry ''data structure'' in the
Encyclopædia Britannica The (Latin for "British Encyclopædia") is a general knowledge English-language encyclopaedia. It is published by Encyclopædia Britannica, Inc.; the company has existed since the 18th century, although it has changed ownership various t ...
(2009
Online entry
Accessed 4 Oct 2011.
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 (elements) it contains. Underlying (inherited) 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 language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s.


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 (last in, first out) order and in the case of queues it is done according to the FIFO (first in, first out) 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 In computer programming, create, read, update, and delete (CRUD) are the four basic operations of persistent storage. CRUD is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing information u ...
-like methods to do the following: * create an empty container (constructor); * insert objects into the container; * delete objects from the container; * delete all the objects in the container (clear); * access the objects in the container; * access the number of objects in the container (count). Containers are sometimes implemented in conjunction with
iterator In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
s.


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 (e.g.
for loop In computer science a for-loop or for loop is a control flow statement for specifying iteration. Specifically, a for loop functions by running a section of code repeatedly until a certain condition has been satisfied. For-loops have two par ...
) or with an
iterator In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
. An associative container uses an
associative array In computer science, an associative array, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In mathematical terms an ...
, 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: * FIFO queues * LIFO stacks * Priority queues * Lookup tables (LUTs) * Key-associated data structures ** Sets, containing and indexing objects by value or by specific property; **
Maps A map is a symbolic depiction emphasizing relationships between elements of some space, such as objects, regions, or themes. Many maps are static, fixed to paper or some other durable medium, while others are dynamic or interactive. Although ...
, associating to each key a "value" for lookup Common data structures used to implement these abstract types include: *
Arrays An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
and their derivatives * Linked lists *
Binary search tree In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and ...
s (BSTs), particularly self-balancing BSTs *
Hash table In computing, a hash table, also known as hash map, is a data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values. A hash table uses a hash function to compute an ''index'', ...
s


Graphic containers

Widget toolkit A widget toolkit, widget library, GUI toolkit, or UX library is a library or a collection of libraries containing a set of graphical control elements (called ''widgets'') used to construct the graphical user interface (GUI) of programs. Most widge ...
s also use containers, which are special widgets to group other widgets, such as
windows Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for ser ...
, 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 Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
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 (e.g. integers or floating numbers) are inherently incompatible with each other because of the memory size they occupy and their semantic meaning and therefore require different containers (unless of course, they are mutually compatible or convertible). Modern programming languages offer various approaches to help solving the problem: :; Universal basic type :: A type that is universally assignable by any other (e.g. root Object class). :;
Downcasting In class-based programming, downcasting or type refinement is the act of casting a reference of a base class to one of its derived classes. In many programming languages, it is possible to check through type introspection to determine whether th ...
; :;Class substitution :: Previous three approaches above are used for weakly typed languages; these usually imply inheritance and polymorphism shared by types. :;
Union type In computer science, a union is a value that may have any of several representations or formats within the same position in memory; that consists of a variable that may hold such a data structure. Some programming languages support special data ...
s (C/C++ language) :: Permits storing types of different data sizes; it is hard to ensure which type is stored in a union upon retrieval however and should be carefully followed. :;
Type conversion In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point valu ...
:;
Template Template may refer to: Tools * Die (manufacturing), used to cut or shape material * Mold, in a molding process * Stencil, a pattern or overlay used in graphic arts (drawing, painting, etc.) and sewing to replicate letters, shapes or designs ...
s or
Generic Generic or generics may refer to: In business * Generic term, a common name used for a range or class of similar things not protected by trademark * Generic brand, a brand for a product that does not have an associated brand or trademark, other ...
s :: Ensures reusability and type safety; may be thought as a reverse inheritance. However, this approach may require implementing a
template specialization Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered by ...
which is reputedly a time-consuming process given that types differ in their methods.


See also

*
List of data structures This is a list of well-known data structures. For a wider list of terms, see list of terms relating to algorithms and data structures. For a comparison of running times for a subset of this list see comparison of data structures. Data types ...
* Standard Template Library#Containers *
Collection (abstract data type) In computer programming, a collection is a grouping of some variable number of data items (possibly zero) that have some shared significance to the problem being solved and need to be operated upon together in some controlled fashion. Generally, ...
* Stack data structure * Java ConcurrentMap


References


External links


Container Data Structure Declaration and Initialization
{{DEFAULTSORT:Container (Data Structure) Abstract data types Object-oriented programming eo:Ujo pt:Container (programação) qu:Wisina