.\" ident @(#)Containers.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH Containers 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2Containers\fP \ - A standard template library (STL) collection. .SH DESCRIPTION Within the standard template library, collection classes are often described as containers. A container stores a collection of other objects and includes basic functions that support the use of generic algorithms. Containers come in two types: sequences and associative containers. They are further distinguished by the type of iterator they support. A sequence supports a linear arrangement of single elements. vector, list, deque, bitset, and string fall into this category. Associative containers map values onto keys, which allows for retrieval of the values based on the keys. The STL includes the map, multimap, set, and multiset associative containers. map and multimap store the value and the key separately, and allow for fast retrieval of the value, based upon fast retrieval of the key. set and multiset store only keys allowing fast retrieval of the key itself. .SH CONTAINER REQUIREMENTS Containers within the STL must meet the following requirements. Sequences and associative containers must also meet their own separate sets of requirements. The requirements for containers are: .HP .5i \(bu A container allocates all storage for the objects it holds. .HP .5i \(bu A container \f2X\fP of objects of type \f2T\fP includes the following types: .HP 0 .HP 16 \f2X::value_type \fPa \f2T\fP .HP 0 .HP 16 \f2X::reference lvalue\fP of \f2T\fP .HP 0 .HP 21 \f2X::const_reference const lvalue\fP of \f2T\fP .HP 0 .HP 14 \f2X::iterator \fPan iterator type pointing to \f2T\fP. \f2X::iterator\fP cannot be an output iterator .HP 0 .HP 20 \f2X::const_iterator \fPan iterator type pointing to const \f2T\fP. \f2X::iterator\fP cannot be an output iterator .HP 0 .HP 21 \f2X::difference_type \fPa signed integral type (must be the same as the distance type for \f2X::iterator\fP and \f2X::const_iterator\fP) .HP 0 .HP 15 \f2X::size_type \fPan unsigned integral type representing any non-negative value of \f2difference_type\fP .HP 0 .HP 20 \f2X::allocator_type \fPtype of allocator used to obtain storage for elements stored in the container .HP 0 .HP .5i \(bu A container includes a default constructor, a copy constructor, an assignment operator, and a full complement of comparison operators (==, !=, <, >, <=, >=). .HP .5i \(bu A container includes the following member functions: .HP 0 .HP 10 \f2begin() \fPReturns an \f2iterator\fP or a \f2const_iterator\fP pointing to the first element in the collection .HP 0 .HP 8 \f2end() \fPReturns an iterator or a const_iterator pointing just beyond the last element in the collection .HP 0 .HP 18 \f2swap(container) \fPSwaps elements between this container and the swap's argument .HP 0 .HP 10 \f2clear() \fPDeletes all the elements in the container .HP 0 .HP 9 \f2size() \fPReturns the number of elements in the collection as a \f2size_type\fP .HP 0 .HP 13 \f2max_size() \fPReturns the largest possible number of elements for this type of container as a \f2size_type\fP .HP 0 .HP 10 \f2empty() \fPReturns \f2true\fP if the container is empty, \f2false\fP otherwise .HP 0 .HP 18 \f2get_allocator() \fPReturns the allocator used by this container .HP 0 .SH REVERSIBLE CONTAINERS A container may be reversible. Essentially, a reversible container includes a reverse iterator that allows traversal of the collection in a direction opposite that of the default iterator. A reversible container must meet the following requirements in addition to those listed above: .HP .5i \(bu A reversible container includes the following types: .HP 0 .HP 22 \f2X::reverse_iterator \fPAn iterator type pointing to \f2T\fP .HP 0 .HP 28 \f2X::const_reverse_iterator \fPAn iterator type pointing to \f2const T\fP .HP 0 .HP .5i \(bu A reversible container includes the following member functions: .HP 0 .HP 11 \f2rbegin() \fPReturns a \f2reverse_iterator\fP or a \f2const_reverse_iterator\fP pointing past the end of the collection .HP 0 .HP 9 \f2rend() \fPReturns a \f2reverse_iterator\fP or a \f2const_reverse_iterator\fP pointing to the first element in the collection .HP 0 .SH SEQUENCES In addition to the requirements for containers, the following requirements hold for sequences: .HP .5i \(bu \f2iterator\fP and \f2const_iterator\fP must be forward iterators, bidirectional iterators or random access iterators. .HP .5i \(bu A sequence includes the following constructors: .HP 0 .HP 10 \f2X(n, t) \fPConstructs a container with \f2n\fP copies of \f2t\fP .HP 0 .HP 10 \f2X(i, j) \fPConstructs a container with elements from the range \f2[i,j)\fP .HP 0 .HP .5i \(bu A sequence includes the following member functions: .HP 0 .HP 14 \f2insert(p,t) \fPInserts the element \f2t\fP in front of the position identified by the iterator \f2p\fP .HP 0 .HP 16 \f2insert(p,n,t) \fPInserts \f2n\fP copies of \f2t\fP in front of the position identified by the iterator \f2p\fP .HP 0 .HP 16 \f2insert(p,i,j) \fPInserts elements from the range \f2[i,j)\fP in front of the position identified by the iterator \f2p\fP .HP 0 .HP 11 \f2erase(q) \fPErases the element pointed to by the iterator \f2q\fP .HP 0 .HP 15 \f2erase(q1,q2) \fPErases the elements in the range \f2[q1,q2)\fP .HP 0 .HP .5i \(bu A sequence may also include the following member functions if they can be implemented with constant time complexity. .HP 0 .HP 10 \f2front() \fPReturns the element pointed to by \f2begin()\fP .HP 0 .HP 9 \f2back() \fPReturns the element pointed to by \f2end() - 1\fP .HP 0 .HP 16 \f2push_front(x) \fPInserts the element \f2x\fP at \f2begin()\fP .HP 0 .HP 15 \f2push_back(x) \fPInserts the element \f2x\fP at \f2end()\fP .HP 0 .HP 14 \f2pop_front() \fPErases the element at \f2begin()\fP .HP 0 .HP 13 \f2pop_back() \fPErases the element at \f2end() - 1\fP .HP 0 .HP 16 \f2operator[](n) \fPReturns the element at \f2a.begin() + n\fP .HP 0 .HP 8 \f2at(n) \fPReturns the element at \f2a.begin() + n\fP; throws out_of_range if \f2n\fP is invalid .HP 0 .SH ASSOCIATIVE CONTAINERS In addition to the requirements for a container, the following requirements hold for associative containers: .HP .5i \(bu For an associative container \f2iterator\fP and \f2const_iterator\fP must be bidirectional iterators. Associative containers are inherently sorted. Their iterators proceed through the container in the non-descending order of keys (where non-descending order is defined by the comparison object that was used to construct the container). .HP .5i \(bu An associative container includes the following types: .HP 0 .HP 14 \f2X::key_type \fPthe type of the \f2Key\fP .HP 0 .HP 17 \f2X::key_compare \fPthe type of the comparison to use to put the keys in order .HP 0 .HP 19 \f2X::value_compare \fPthe type of the comparison used on values .HP 0 .HP .5i \(bu The default constructor and copy constructor for associative containers use the template parameter comparison class. .HP .5i \(bu An associative container includes the following additional constructors: .HP 0 .HP 7 \f2X(c) \fPConstructs an empty container using \f2c\fP as the comparison object .HP 0 .HP 11 \f2X(i,j,c) \fPConstructs a container with elements from the range \f2[i,j)\fP and the comparison object \f2c\fP .HP 0 .HP 10 \f2X(i, j) \fPConstructs a container with elements from the range [i,j) using the template parameter comparison object .HP 0 .HP .5i \(bu An associative container includes the following member functions: .HP 0 .HP 13 \f2key_comp() \fPReturns the comparison object used in constructing the associative container .HP 0 .HP 15 \f2value_comp() \fPReturns the value comparison object used in constructing the associative container .HP 0 .HP 12 \f2insert(t) \fPIf the container does NOT support redundant key values, then this function only inserts \f2t\fP if there is no key present that is equal to the key of \f2t\fP. If the container DOES support redundant keys, then this function always inserts the element \f2t\fP. Returns a \f2pair\fP. The \f2bool\fP component of the returned pair indicates the success or failure of the operation and the \f2iterator\fP component points to the element with key equal to key of \f2t\fP. .HP 0 .HP 14 \f2insert(p,t) \fPIf the container does NOT support redundant key values, then this function only inserts \f2t\fP if there is no key present that is equal to the key of \f2t\fP. If the container DOES support redundant keys, then this function always inserts the element \f2t\fP. The iterator \f2p\fP serves as a hint of where to start searching, allowing for some optimization of the insertion. It does not restrict the algorithm from inserting ahead of that location if necessary. .HP 0 .HP 14 \f2insert(i,j) \fPInserts elements from the range \f2[i,j). \fPA prerequisite is that \f2i\fP and \f2j\fP cannot be iterators into the container. .HP 0 .HP 11 \f2erase(k) \fPErases all elements with key equal to \f2k\fP. Returns number of erased elements. .HP 0 .HP 11 \f2erase(q) \fPErases the element pointed to by \f2q\fP .HP 0 .HP 15 \f2erase(q1,q2) \fPErases the elements in the range \f2[q1,q2)\fP .HP 0 .HP 10 \f2find(k) \fPReturns an iterator pointing to an element with key equal to \f2k\fP or \f2end()\fP, if such an element is not found .HP 0 .HP 11 \f2count(k) \fPReturns the number of elements with key equal to \f2k\fP .HP 0 .HP 17 \f2lower_bound(k) \fPReturns an iterator pointing to the first element with a key greater than or equal to \f2k\fP .HP 0 .HP 17 \f2upper_bound(k) \fPReturns an iterator pointing to the first element with a key greater than \f2k\fP .HP 0 .HP 17 \f2equal_range(k) \fPReturns a pair of iterators such that the first element of the pair is equivalent to \f2lower_bound(k)\fP and the second element equivalent to \f2upper_bound(k)\fP .HP 0 .SH SEE ALSO bitset, deque, list, map, multimap, multiset, priority_queue, queue, set, stack, vector