Container classes, or containers for short, manage a collection
of elements. To meet different needs, the STL provides different kinds of
containers, as shown in figure below
Figure: STL Container Types
There are two general kinds of containers:
-
Sequence containers are ordered collections in which every element has a certain position. This position depends on the time and place of the insertion, but it is independent of the value of the element. For example, if you put six elements into a collection by appending each element at the end of the actual collection, these elements are in the exact order in which you put them. The STL contains three predefined sequence container classes: vector, deque, and list.
-
Associative containers are sorted collections in which the actual position of an element depends on its value due to a certain sorting criterion. If you put six elements into a collection, their order depends only on their value. The order of insertion doesn't matter. The STL contains four predefined associative container classes: set, multiset, map, and multimap.
An associative container can be considered a special kind of sequence
container because sorted collections are ordered according to a sorting
criterion. You might expect this especially if you have used other libraries of
collection classes like those in Smalltalk or the NIHCL, in which sorted collections are derived
from ordered collections. However, note that the STL collection types are
completely distinct from each other. They have different implementations that
are not derived from each other.
The automatic sorting of elements in associative containers does not
mean that those containers are especially designed for sorting elements. You can
also sort the elements of a sequence container. The key advantage of automatic
sorting is better performance when you search elements. In particular, you can
always use a binary search, which results in logarithmic complexity rather than
linear complexity. For example, this means that for a search in a collection of
1,000 elements you need, on average, only 10 instead of 500 comparisons. Thus, automatic sorting is only
a (useful) "side effect" of the implementation of an associative container,
designed to enable better performance.
The following subsections discuss the different container classes in detail.
Among other aspects, they describe how containers are typically implemented.
Strictly speaking, the particular implementation of any container is not defined
inside the C++ standard library. However, the behavior and complexity specified
by the standard do not leave much room for variation. So, in practice, the
implementations differ only in minor details. Coming post with covers the exact behavior of the
container classes. It describes their common and individual abilities, and
member functions in detail.
Sequence Containers
The following sequence containers are predefined in the STL:-
Vectors
-
Deques
- Lists
Vectors
A vector manages its elements in a dynamic array. It enables random access,
which means you can access each element directly with the corresponding index.
Appending and removing elements at the end of the array is very fast. However, inserting an
element in the middle or at the beginning of the array takes time because all
the following elements have to be moved to make room for it while maintaining
the order. Strictly speaking,
appending elements is amortized very fast. An individual append may be
slow, when a vector has to reallocate new memory and to copy existing elements
into the new memory. However, because such reallocations are rather rare, the
operation is very fast in the long term.
The following example defines a vector for integer values, inserts six
elements, and prints the elements of the vector:
-----------------------------------------------------------------
See Also:
-----------------------------------------------------------------
See Also:
-----------------------------------------------------------------
- Complete Tutorial of C++ Template's
- Standard Template Library Tutorial
- Inter Process Communication Tutorial
- Advance Programming in C & C++
-----------------------------------------------------------------
No comments:
Post a Comment