Associative Containers
Associative containers are effective in searching for elements based on keys. For example: You may want to obtain the name of an employee using his ID number. In this case, ID number is the key based on which you will search and retrieve the name.
These containers will store the elements based on the value of the key (i.e. they may store the elements in ascending order of their keys). This is what permits efficient searching of associative containers. Since they depend on the key value, generally you cannot modify the value of the key itself. Associative containers can be divided into two types:
Pair associative is the example we discussed about employee ID and employee name. In this case the part of the element which is the key (i.e. the ID number) cannot be modified but the other part (i.e. the employee name) can be changed.
In simple associative containers we store the entire element as the key. Thus you cannot modify the elements of a simple associative container.
Again within the simple and pair associative containers, we have two more sub divisions:
In the unique key type, the key cannot be repeated (i.e. no two elements can have the same key). In multiple key type, many elements can have the same key value. For example: in a college, each student will take up lots of courses. Thus the student ID number is the same but there will be many subjects under that ID.
To permit quick retrieval of data, the elements are arranged in a tree structure (the binary tree structure aids in quick retrieval of data).
Some common functions available in all associative containers are listed below.
Member function |
Use |
void erase ( ) |
3 forms available - Erases a particular element based on key value or based on a reference or a range of elements. |
void clear( ) |
Erases all the elements of the container. |
iterator find (x) |
The value of the key to be searched is passed as argument. |
size_type count(x) |
Returns the number of elements with the key value ‘x’. |
pair equal_range(x) |
Returns a pair of iterators specifying the range of elements having the key value as x. |
Note: italics denotes the return data type.
Sorted associative containers contain some more member functions in common. The 4 containers we will discuss about are:
Set
Multi-set
Map
Multi-map
All four are sorted containers.
Go to the next section on: Set Container
Go back to Contents page 2.
Copyright © 2004 Sethu Subramanian All rights reserved.