General Theory of Operation. McGraw-Hill The Complete Reference Fourth Edition
Annons
Annons
634
C + + : T h e C o m p l e t e R e f e r e n c e
Since the names of the generic placeholder types in a template class declaration are arbitrary, the container classes declare typedefed versions of these types. This makes the type names concrete. Some of the most common typedef names are shown here:
size_type reference const_reference iterator const_iterator reverse_iterator
Some type of integer
A reference to an element
A const reference to an element
An iterator
A const iterator
A reverse iterator
const_reverse_iterator
A const reverse iterator
value_type allocator_type
The type of a value stored in a container
The type of the allocator
key_type key_compare value_compare
The type of a key
The type of a function that compares two keys
The type of a function that compares two values
General Theory of Operation
Although the internal operation of the STL is highly sophisticated, to use the STL is actually quite easy. First, you must decide on the type of container that you wish to use. Each offers certain benefits and trade-offs. For example, a vector is very good when a random-access, array-like object is required and not too many insertions or deletions are needed. A list offers low-cost insertion and deletion but trades away speed. A map provides an associative container, but of course incurs additional overhead.
Once you have chosen a container, you will use its member functions to add elements to the container, access or modify those elements, and delete elements. Except for bitset, a container will automatically grow as needed when elements are added to it and shrink when elements are removed.
Elements can be added to and removed from a container a number of different ways. For example, both the sequence containers (vector, list, and deque) and the associative containers (map, multimap, set, and multiset) provide a member function called insert( ), which inserts elements into a container, and erase( ), which removes elements from a container. The sequence containers also provide push_back( ) and pop_back( ), which add an element to or remove an element from the end, respectively. These functions are probably the most common way that individual elements are added to or removed from a sequence container. The list and deque
Ladda ner
Annons