Selecting a container – HP Integrity NonStop H-Series User Manual
Page 49
![background image](/manuals/396950/49/background.png)
Click on the banner to return to the user guide home page.
©Copyright 1996 Rogue Wave Software
Selecting a Container
The following series of questions can help you determine which type of container is best suited
for solving a particular problem.
How are values going to be accessed?
If random access is important, than a
vector
or a
deque
should be used. If sequential access is
sufficient, then one of the other structures may be suitable.
Is the order in which values are maintained in the collection important?
There are a number of different ways values can be sequenced. If a strict ordering is important
throughout the life of the container, then the
set
data structure is an obvious choice, as insertions
into a set are automatically placed in order. On the other hand, if this ordering is important only
at one point (for example, at the end of a long series of insertions), then it might be easier to
place the values into a
list
or
vector
, then sort the resulting structure at the appropriate time. If
the order that values are held in the structure is related to the order of insertion, then a
stack
,
queue
, or list may be the best choice.
Will the size of the structure vary widely over the course of execution?
If true, then a
list
or
set
might be the best choice. A
vector
or
deque
will continue to maintain a
large buffer even after elements have been removed from the collection. Conversely, if the size
of the collection remains relatively fixed, than a vector or deque will use less memory than will
a list or set holding the same number of elements.
Is it possible to estimate the size of the collection?
The
vector
data structure provides a way to pre-allocate a block of memory of a given size
(using the reserve() member function). This ability is not provided by the other containers.
Is testing to see whether a value is contained in the collection a frequent operation?
If so, then the
set
or
map
containers would be a good choice. Testing to see whether a value is
contained in a set or map can be performed in a very small number of steps (logarithmic in the
size of the container), whereas testing to see if a value is contained in one of the other types of
collections might require comparing the value against every element being stored by the
container.