25 Nov 2019

  • November 25, 2019
  • Amitraj
Containers:-

Containers are objects that hold data. The STL defines Ten containers which are grouped into three categories.






1. Sequence Containers

    Sequence containers store elements in a linear sequence, like a line as shown in fig. each element is related to other elements by its position along the line. They all expand themselves to allow insertion of elements and all of them support a number of operations on them.






The STL provides 3 types of sequence containers:
1. Vector
2. list
3. deque

-> Elements in all these containers can be accessed using an iterator. The difference between the 3 of them is related to only their performance.


2. Associative Containers

These containers are designed to support direct access to elements using keys. They are not sequential. There are four types of associative containers:

1. Set
2. Multiset
3. Map
4. Multimap

-> All these containers store data in a structure called tree which facillitates fast searching, deletion, and insertion. However, these are very slow for random access and inefficient for sorting.

-> Containers set and multiset can store a number of items and provide operations for manipulating them using the values as the keys.

-> Containers map and multimap are used to store pairs of items, one called the key and the other called the value. The main difference between map and multimap is that a map allows only one key for a given value to be stored while multimap permits multiple keys.


3. Derived Containers

The STL provides three derived containers namely; stack, queue, priority_queue. These are also known as container adaptors.

-> stack , queues and priority_queues can be created from different sequence containers. The derived containers do not support iterators and therefore we can not use them for data manipulation. However , they support two member functions pop() and push() for implementing, deleting and inserting operations.




Containers supported by STL

Containers
Description
Header file
Iterator
Vector
It can be defined as a dynamic array. It permits direct access to any element.
<vector>
Random access
List
It is a  bidirectional linear list. It allows insertion and deletion anywhere
<list>
Bidirectional
deque
It is a double-ended queue. Allows insertions and deletions at both the ends. Permits direct access to any element.
<deque>
Random access
set
It is an associate container for storing unique sets. Allows rapid lookup.
<set>
Bidirectional
multiset
It is an associate container for storing non-unique sets.
<set>
Bidirectional
map
It is an associate container for storing unique key/value pairs. Each key is associated with only one value.
<map>
Bidirectional
multimap
It is an associate container for storing key/value in which one key may be associated with more than one value (one-to-many mapping). It allows a key-based lookup.
<map>
Bidirectional
stack
A standard stack follows last-in-first-out(LIFO)
<stack>
No iterator
queue
A standard queue follows first-in-first-out(FIFO)
<queue>
No iterator
priority-queue
The first element out is always the highest priority element
<queue>
No iterator




Applications of Containers classes

1. Container classes make programmers more productive.

2. Container classes let programmers write more robust code.

3. The vector is the most widely used container. it stores element in continguous memory locations and enables direct access using subscript operator []. A vector can change its size dynamically and therefore allocates memory as needed at run time.

4. Lists supports bidirectional, linear list and provides an efficient implementation for deletion and insertion operations. unlike a vector, which supports random access, A list can be accessed sequentially only.

5. Map is a sequence of (key,value) pairs where a single value is associated with each unique key. A map is a commonly called an associative array. The key is specified using the subscript operator[].  


Translate

Popular Posts