.\" ident @(#)map.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH map 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2map\fP \ - An associative container with access to non-key values using unique keys. A map supports bidirectional iterators. .SH SYNOPSIS .br #include .br template .RE .RS 9 class Allocator = allocator> > .RE .RS 0 class map; .SH DESCRIPTION map_ gives fast access to stored values of type \f2T\fP that are indexed by unique keys of type Key. The default operation for key comparison is the \f2<\fP operator. map has bidirectional iterators that point to an instance of \f2pair\fP where \f2x\fP is the key and \f2y\fP is the stored value associated with that key. The definition of map includes a \f2typedef\fP to this pair called \f2value_type\fP. The types used for both the template parameters \f2Key\fP and\f2 T\fP must include the following (where \f2T\fP is the \f2type\fP, \f2t\fP is a \f2value\fP of \f2T\fP and \f2u\fP is a \f2const\fP \f2value\fP of \f2T\fP): .HP 20 Copy constructors \f2T(t)\fP and \f2T(u)\fP .HP 0 .HP 13 Destructor \f2t.~T()\fP .HP 0 .HP 13 Address of \f2&t\fP and \f2&u\fP yielding \f2T*\fP and \f2const T*\fP respectively .HP 0 .HP 13 Assignment \f2t = a\fP where \f2a\fP is a (possibly \f2const\fP) value of \f2T\fP .HP 0 The type used for the \f2Compare\fP template parameter must satisfy the requirements for binary functions. .SH INTERFACE .br template .RE .RS 9 class Allocator = allocator> > .RE .RS 0 class map { .br .br public: .br .br // types .br .RE .RS 2 typedef Key key_type; .br typedef typename Allocator::pointer pointer; .br typedef typename Allocator::const_pointer const_pointer; .br typedef T mapped_type; .br typedef pair value_type; .br typedef Compare key_compare; .br typedef Allocator allocator_type; .br typedef typename .RE .RS 10 Allocator::reference reference; .RE .RS 2 typedef typename .RE .RS 10 Allocator::const_reference const_reference; .RE .RS 2 class iterator; .br class const_iterator; .br typedef typename .RE .RS 10 Allocator::size_type size_type; .RE .RS 2 typedef typename .RE .RS 10 Allocator::difference_type difference_type; .RE .RS 2 typedef typename std::reverse_iterator .RE .RS 24 reverse_iterator; .RE .RS 2 typedef typename std::reverse_iterator .RE .RS 24 const_reverse_iterator; .RE .RS 0 .RE .RS 2 class value_compare .RE .RS 6 : public binary_function .RE .RS 3 { .RE .RS 4 friend class map; .RE .RS 0 .RE .RS 4 protected : .RE .RS 6 Compare comp; .br value_compare(Compare c): comp(c) {} .RE .RS 4 public : .RE .RS 6 bool operator() (const value_type&, .RE .RS 23 const value_type&) const; .RE .RS 3 }; .RE .RS 0 .br // Construct/Copy/Destroy .br .RE .RS 2 explicit map (const Compare& = Compare(), .RE .RS 16 const Allocator& = Allocator ()); .RE .RS 2 template .RE .RS 3 map (InputIterator, InputIterator, .RE .RS 8 const Compare& = Compare(), .br const Allocator& = Allocator ()); .RE .RS 2 map (const map&); .RE .RS 3 ~map(); .RE .RS 2 map& .RE .RS 3 operator= (const map&); .RE .RS 2 allocator_type get_allocator () const; .RE .RS 0 .br // Iterators .br .RE .RS 2 iterator begin(); .br const_iterator begin() const; .br iterator end(); .br const_iterator end() const; .br reverse_iterator rbegin(); .br const_reverse_iterator rbegin() const; .br reverse_iterator rend(); .br const_reverse_iterator rend() const; .RE .RS 0 .br // Capacity .br .RE .RS 2 bool empty() const; .br size_type size() const; .br size_type max_size() const; .RE .RS 0 .br // Element Access .br .RE .RS 2 mapped_type& operator[] (const key_type&); .RE .RS 0 .br // Modifiers .br .RE .RS 2 pair insert (const value_type&); .br iterator insert (iterator, const value_type&); .br template .RE .RS 3 void insert (InputIterator, InputIterator); .RE .RS 0 .RE .RS 2 void erase (iterator); .br size_type erase (const key_type&); .br void erase (iterator, iterator); .br void swap (map&); .br void clear(); .RE .RS 0 .br // Observers .br .RE .RS 2 key_compare key_comp() const; .br value_compare value_comp() const; .RE .RS 0 .br // Map operations .br .RE .RS 2 iterator find (const key_value&); .br const_iterator find (const key_value&) const; .br size_type count (const key_type&) const; .br iterator lower_bound (const key_type&); .br const_iterator lower_bound (const key_type&) const; .br iterator upper_bound (const key_type&); .br const_iterator upper_bound (const key_type&) const; .br pair equal_range (const key_type&); .br pair .RE .RS 4 equal_range (const key_type&) const; .RE .RS 0 }; .br .br // Non-member Map Operators .br .br template .RE .RS 1 \f2bool\fP operator== (const map&, .RE .RS 17 const map&); .RE .RS 0 .br template .RE .RS 1 \f2bool\fP operator!= (const map&, .RE .RS 17 const map&); .RE .RS 0 .br template .br bool operator< (const map&, .RE .RS 16 const map&); .RE .RS 0 .br template .br bool operator> (const map&, .RE .RS 16 const map&); .RE .RS 0 .br template .br bool operator<= (const map&, .RE .RS 16 const map&); .RE .RS 0 .br template .br bool operator>= (const map&, .RE .RS 16 const map&); .RE .RS 0 .br .br // Specialized Algorithms .br .br template .br void swap (map<*Key,T,Compare,Allocator>&, .RE .RS 11 map&); .SH CONSTRUCTORS .RE .RS 0 explicit map(const Compare& comp = Compare(), .RE .RS 12 const Allocator& alloc = Allocator()); .RE .RS 3 Constructs an empty map that uses the relation \f2comp\fP to order keys, if it is supplied. The map uses the allocator \f2alloc\fP for all storage management. .RE .RE .RS 0 template .br map(InputIterator first, InputIterator last, .RE .RS 3 const Compare& comp = Compare(), .br const Allocator& alloc = Allocator()); .RE .RS 3 Constructs a map containing values in the range \f2[first, last)\fP. Creation of the new map is only guaranteed to succeed if the iterators \f2first\fP and \f2last\fP return values of type \f2pair\fP and all values of \f2Key\fP in the \f2range[first, last)\fP are unique. The map uses the relation \f2comp\fP to order keys, and the allocator \f2alloc\fP for all storage management. .RE .RE .RS 0 map(const map& x); .RE .RS 3 Creates a new map by copying all pairs of \f2key\fP and \f2value\fP from \f2x\fP. .RE .SH DESTRUCTORS .br ~map(); .RE .RS 3 Releases any allocated memory for this map. .RE .SH ALLOCATORS .br allocator_type get_allocator() const; .RE .RS 3 Returns a copy of the allocator used by self for storage management. .RE .SH ITERATORS .br iterator .br begin(); .RE .RS 3 Returns an \f2iterator\fP pointing to the first element stored in the map. "First" is defined by the map's comparison operator, \f2Compare\fP. .RE .br const_iterator .br begin() const; .RE .RS 3 Returns a \f2const_iterator\fP pointing to the first element stored in the map. .RE .br iterator .br end(); .RE .RS 3 Returns an \f2iterator\fP pointing to the last element stored in the map (in other words, the off-the-end value). .RE .br const_iterator .br end() const; .RE .RS 3 Returns a \f2const_iterator\fP pointing to the last element stored in the map. .RE .br reverse_iterator .br rbegin(); .RE .RS 3 Returns a \f2reverse_iterator\fP pointing to the first element stored in the map. "First" is defined by the map's comparison operator, \f2Compare\fP. .RE .br const_reverse_iterator .br rbegin() const; .RE .RS 3 Returns a \f2const_reverse_iterator\fP pointing to the first element stored in the map. .RE .br reverse_iterator .br rend(); .RE .RS 3 Returns a \f2reverse_iterator \fPpointing to the last element stored in the map (in other words, the off-the-end value). .RE .br const_reverse_iterator .br rend() const; .RE .RS 3 Returns a \f2const_reverse_iterator\fP pointing to the last element stored in the map. .RE .SH MEMBER OPERATORS .br map& .br operator=(const map& x); .RE .RS 3 Replaces the contents of \f2*this\fP with a copy of the map \f2x\fP. .RE .br mapped_type& .br operator[](const key_type& x); .RE .RS 3 If an element with the key \f2x\fP exists in the map, then a reference to its associated value is returned. Otherwise the pair \f2x,T()\fP is inserted into the map and a reference to the default object \f2T()\fP is returned. .RE .SH MEMBER FUNCTIONS .br void .br clear(); .RE .RS 3 Erases all elements from the self. .RE .br size_type .br count(const key_type& x) const; .RE .RS 3 Returns a \f21\fP if a value with the key \f2x\fP exists in the map. Otherwise returns a \f20\fP. .RE .br bool .br empty() const; .RE .RS 3 Returns \f2true\fP if the map is empty, \f2false\fP otherwise. .RE .br pair .br equal_range (const key_type& x); .RE .RS 3 Returns the pair \f2(lower_bound(x), upper_bound(x))\fP. .RE .br pair .br equal_range (const key_type& x) const; .RE .RS 3 Returns the pair \f2(lower_bound(x), upper_bound(x))\fP. .RE .br void .br erase(iterator position); .RE .RS 3 Deletes the map element pointed to by the iterator \f2position\fP. Returns an iterator pointing to the element following the deleted element, or \f2end()\fP if the deleted item was the last one in this list. .RE .br void .br erase(iterator first, iterator last); .RE .RS 3 If the iterators \f2first\fP and \f2last\fP point to the same map and last is reachable from first, all elements in the range (\f2first, last\fP) are deleted from the map. Returns an iterator pointing to the element following the last deleted element, or \f2end()\fP if there were no elements after the deleted range. .RE .br size_type .br erase(const key_type& x); .RE .RS 3 Deletes the element with the key value \f2x\fP from the map, if one exists. Returns \f21\fP if \f2x\fP existed in the map, \f20\fP otherwise. .RE .br iterator .br find(const key_type& x); .RE .RS 3 Searches the map for a pair with the key value \f2x\fP and returns an \f2iterator\fP to that pair if it is found. If such a pair is not found the value \f2end()\fP is returned. .RE .br const_iterator find(const key_type& x) const; .RE .RS 3 Same as \f2find\fP above but returns a \f2const_iterator\fP. .RE .br pair .br insert(const value_type& x); .br iterator .br insert(iterator position, const value_type& x); .RE .RS 3 If a \f2value_type\fP with the same key as \f2x\fP is not present in the map, then \f2x\fP is inserted into the map. Otherwise, the pair is not inserted. A position may be supplied as a hint regarding where to do the insertion. If the insertion is done right after \f2position\fP, then it takes amortized constant time. Otherwise it takes\f2 O(log N)\fP time. .RE .br template .br void .br insert(InputIterator first, InputIterator last); .RE .RS 3 Copies of each element in the range \f2[first, last)\fP that possess a unique key (one not already in the map) are inserted into the map. The iterators \f2first\fP and \f2last\fP must return values of \f2type\fP \f2pair\fP. This operation takes approximately\f2 O(N*log(size()+N)) \fPtime. .RE .br key_compare .br key_comp() const; .RE .RS 3 Returns a function object capable of comparing key values using the comparison operation, \f2Compare\fP, of the current map. .RE .br iterator .br lower_bound(const key_type& x); .RE .RS 3 Returns a reference to the first entry with a key greater than or equal to \f2x\fP. .RE .br const_iterator .br lower_bound(const key_type& x) const; .RE .RS 3 Same as \f2lower_bound\fP above but returns a \f2const_iterator\fP. .RE .br size_type .br max_size() const; .RE .RS 3 Returns the maximum possible size of the map. This size is only constrained by the number of unique keys that can be represented by the type \f2Key\fP. .RE .br size_type .br size() const; .RE .RS 3 Returns the number of elements in the map. .RE .br void .br swap(map& x); .RE .RS 3 Swaps the contents of the map \f2x\fP with the current map, \f2*this\fP. .RE .br iterator .br upper_bound(const key_type& x); .RE .RS 3 Returns a reference to the first entry with a key less than or equal to \f2x\fP. .RE .br const_iterator .br upper_bound(const key_type& x) const; .RE .RS 3 Same as \f2upper_bound\fP above but returns a \f2const_iterator.\fP .RE .br value_compare .br value_comp() const; .RE .RS 3 Returns a function object capable of comparing \f2pair\fP values using the comparison operation,\f2 Compare\fP, of the current map. This function is identical to \f2key_comp\fP for sets. .RE .SH NON-MEMBER OPERATORS .br template .br bool operator==(const map& x, .RE .RS 16 const map& y); .RE .RS 3 Returns \f2true\fP if all elements in \f2x\fP are element-wise equal to all elements in \f2y\fP, using\f2 (T::operator==)\fP. Otherwise it returns \f2false\fP. .RE .RE .RS 0 template .br bool operator!=(const map& x, .RE .RS 16 const map& y); .RE .RS 3 Returns !\f2(x==y)\fP. .RE .RE .RS 0 template .br bool operator<(const map& x, .RE .RS 15 const map& y); .RE .RS 3 Returns \f2true\fP if \f2x\fP is lexicographically less than \f2y\fP. Otherwise, it returns \f2false\fP. .RE .RE .RS 0 template .br bool operator>(const map& x, .RE .RS 15 const map& y); .RE .RS 3 Returns \f2y < x\fP. .RE .RE .RS 0 template .br bool operator<=(const map& x, .RE .RS 15 const map& y); .RE .RS 3 Returns !\f2(y < x)\fP. .RE .RE .RS 0 template .br bool operator>=(const map& x, .RE .RS 15 const map& y); .RE .RS 3 Returns !\f2(x < y)\fP. .RE .SH SPECIALIZED ALGORITHMS .RE .RS 0 template .br void swap(map& a, .RE .RS 10 map& b); .RE .RS 3 Swaps the contents of \f2a\fP and \f2b\fP. .RE .SH EXAMPLE .RE .RS 0 // .br // map.cpp .br // .RE .RS 1 #include .br #include .br #include .RE .RS 0 using namespace std; .br .br typedef map > months_type; .br .RE .RS 1 // Print out a pair .RE .RS 0 template .br ostream& operator<<(ostream& out, .RE .RS 20 const pair & p) .RE .RS 1 { .RE .RS 2 cout << p.first << " has " << p.second << " days"; .br return out; .RE .RS 1 } .RE .RS 0 .RE .RS 1 // Print out a map .RE .RS 0 ostream& operator<<(ostream& out, const months_type & l) .RE .RS 1 { .RE .RS 2 copy(l.begin(),l.end(), ostream_iterator .RE .RS 16 (cout,"\\n")); .RE .RS 2 return out; .RE .RS 1 } .RE .RS 0 .br .br int main(void) .RE .RS 1 { .RE .RS 3 // create a map of months and the number of days .br // in the month .RE .RS 2 months_type months; .RE .RS 0 .RE .RS 2 typedef months_type::value_type value_type; .RE .RS 0 .RE .RS 3 // Put the months in the multimap .RE .RS 2 months.insert(value_type(string("January"), 31)); .br months.insert(value_type(string("February"), 28)); .br months.insert(value_type(string("February"), 29)); .br months.insert(value_type(string("March"), 31)); .br months.insert(value_type(string("April"), 30)); .br months.insert(value_type(string("May"), 31)); .br months.insert(value_type(string("June"), 30)); .br months.insert(value_type(string("July"), 31)); .br months.insert(value_type(string("August"), 31)); .br months.insert(value_type(string("September"), 30)); .br months.insert(value_type(string("October"), 31)); .br months.insert(value_type(string("November"), 30)); .br months.insert(value_type(string("December"), 31)); .RE .RS 0 .RE .RS 3 // print out the months .br // Second February is not present .RE .RS 2 cout << months << endl; .RE .RS 0 .RE .RS 3 // Find the Number of days in June .RE .RS 2 months_type::iterator p = months.find(string("June")); .RE .RS 0 .RE .RS 3 // print out the number of days in June .RE .RS 2 if (p != months.end()) .RE .RS 4 cout << endl << *p << endl; .RE .RS 1 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br April has 30 days .br August has 31 days .br December has 31 days .br February has 28 days .br January has 31 days .br July has 31 days .br June has 30 days .br March has 31 days .br May has 31 days .br November has 30 days .br October has 31 days .br September has 30 days .SH WARNINGS Member function templates are used in all containers included in the Standard Template Library. An example of this feature is the constructor for \f2map\fP that takes two templatized iterators: .br template .br map (InputIterator, InputIterator, .RE .RS 5 const Compare& = Compare(), .br const Allocator& = Allocator()); .RE map also has an insert function of this type. These functions, when not restricted by compiler limitations, allow you to use any type of input iterator as arguments. For compilers that do not support this feature, substitute functions allow you to use an iterator obtained from the same type of container as the one you are constructing (or calling a member function on), or you can use a pointer to the type of element you have in the container. For example, if your compiler does not support member function templates, you can construct a map in the following two ways: .RE .RS 0 map >::value_type intarray[10]; .br map > first_map(intarray, .RE .RS 35 intarray + 10); .RE .RS 0 map > second_map(first_map.begin(), .RE .RS 36 first_map.end()); .RE But not this way: .RE .RS 0 map > long_map(first_map.begin(), .RE .RS 37 first_map.end()); .RE Since the \f2long_map\fP and \f2first_map\fP are not the same type. Also, many compilers do not support default template arguments. If your compiler is one of these, you always need to supply the \f2Compare\fP template argument and the \f2Allocator\fP template argument. For instance, you have to write: \f2map, allocator >\fP instead of: \f2map\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO allocator, Containers, Iterators, multimap