.\" ident @(#)multimap.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH multimap 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2multimap\fP \ - An associative container that gives access to non-key values using keys. multimap keys are not required to be unique. A multimap supports bidirectional iterators. .SH SYNOPSIS .br #include .br template , .RE .RS 9 class Allocator = allocator> > .RE .RS 0 class multimap; .SH DESCRIPTION multimap_ gives fast access to stored values of type \f2T \fPthat are indexed by keys of type \f2Key\fP. The default operation for key comparison is the \f2<\fP operator. Unlike map, multimap allows insertion of duplicate keys. multimap uses 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 multimap includes a \f2typedef\fP to this pair called \f2value_type\fP. The types used for both the template parameters \f2Key\fP and \f2T\fP must include the following (where \f2T\fP is the \f2type\fP, \f2t\fP is a value 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 multimap { .br .br public: .br .br // types .br .RE .RS 2 typedef Key key_type; .br typedef T mapped_type; .br typedef pair value_type; .br typedef Compare key_compare; .br typedef Allocator allocator_type; .RE .RS 0 .RE .RS 2 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 .br class value_compare .RE .RS 4 : public binary_function .br { .br friend class multimap; .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 4 }; .RE .RS 0 .br // Construct/Copy/Destroy .br .RE .RS 2 explicit multimap (const Compare& = Compare(), .RE .RS 21 const Allocator& = .br Allocator()); .RE .RS 2 template .RE .RS 3 multimap (InputIterator, InputIterator, .RE .RS 13 const Compare& = Compare(), .br const Allocator& = Allocator()); .RE .RS 2 multimap (const multimap&); .RE .RS 3 ~multimap (); .RE .RS 2 multimap& operator= .RE .RS 7 (const multimap&); .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 // Modifiers .br .RE .RS 2 iterator 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 (multimap&); .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 // Multimap operations .br .RE .RS 2 iterator find (const key_type&); .br const_iterator find (const key_type&) const; .br size_type count (const key_type&) const; .RE .RS 0 .RE .RS 2 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 // Non-member Operators .br .br template .RE .RS 0 bool operator== (const multimap&, .br const multimap&); .RE .RS 0 .br template .RE .RS 0 bool operator!= (const multimap&, .br const multimap&); .RE .RS 0 .br template .RE .RS 0 bool operator< (const multimap&, .br const multimap&); .RE .RS 0 .br template .RE .RS 0 bool operator> (const multimap&, .br const multimap&); .RE .RS 0 .br template .RE .RS 0 bool operator<= (const multimap&, .br const multimap&); .RE .RS 0 .br template .RE .RS 0 bool operator>= (const multimap&, .br const multimap&); .RE .RS 0 .br // Specialized Algorithms .br .br template .RE .RS 0 void swap (multimap&, .RE .RS 11 multimap&; .SH CONSTRUCTORS .RE .RS 0 explicit multimap(const Compare& comp = Compare(), .RE .RS 17 const Allocator& alloc = Allocator()); .RE .RS 3 Constructs an empty multimap that uses the optional relation \f2comp\fP to order keys and the allocator \f2alloc\fP for all storage management. .RE .RE .RS 0 template .br multimap(InputIterator first, .RE .RS 9 InputIterator last, .br const Compare& comp = Compare() .br const Allocator& alloc = Allocator()); .RE .RS 3 Constructs a multimap containing values in the range \f2[first, last)\fP. Creation of the new multimap is only guaranteed to succeed if the iterators \f2first\fP and \f2last\fP return values of type\f2 pair.\fP .RE .RE .RS 0 multimap(const multimap& x); .RE .RS 3 Creates a new multimap by copying all pairs of \f2key\fP and \f2value\fP from \f2x\fP. .RE .SH DESTRUCTORS .br ~multimap(); .RE .RS 3 Releases any allocated memory for this multimap. .RE .SH ASSIGNMENT OPERATORS .br multimap& .br operator=(const multimap& x); .RE .RS 3 Replaces the contents of \f2*this\fP with a copy of the multimap\f2 x\fP. .RE .SH ALLOCATORS .br allocator_type .br 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 a bidirectional \f2iterator\fP pointing to the first element stored in the multimap. "First" is defined by the multimap's comparison operator, \f2Compare\fP. .RE .br const_iterator .br begin() const; .RE .RS 3 Returns a\f2 const_iterator\fP pointing to the first element stored in the multimap. "First" is defined by the multimap's comparison operator, \f2Compare\fP. .RE .br iterator .br end(); .RE .RS 3 Returns a bidirectional \f2iterator\fP pointing to the last element stored in the multimap (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 multimap. .RE .br reverse_iterator .br rbegin(); .RE .RS 3 Returns a \f2reverse_iterator\fP pointing to the first element stored in the multimap. "First" is defined by the multimap'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 multimap. .RE .br reverse_iterator .br rend(); .RE .RS 3 Returns a \f2reverse_iterator\fP pointing to the last element stored in the multimap (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 multimap. .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 the number of elements in the multimap with the key value \f2x\fP. .RE .br bool .br empty() const; .RE .RS 3 Returns \f2true\fP if the multimap is empty, \f2false\fP otherwise. .RE .br pair .br equal_range(const key_type& x); .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 first, iterator last); .RE .RS 3 If the iterators \f2first\fP and \f2last\fP point to the same multimap and last is reachable from first, all elements in the range (\f2first, last\fP) are deleted from the multimap. Returns an \f2iterator\fP pointing to the element following the last deleted element or \f2end(),\fP if there were no elements after the deleted range. .RE .br void .br erase(iterator position); .RE .RS 3 Deletes the multimap element pointed to by the iterator \f2position\fP. Returns an \f2iterator\fP pointing to the element following the deleted element, or \f2end(), \fPif the deleted item was the last one in this list. .RE .br size_type .br erase(const key_type& x); .RE .RS 3 Deletes the elements with the key value \f2x\fP from the map, if any exist. Returns the number of deleted elements, or \f20\fP otherwise. .RE .br iterator .br find(const key_type& x); .RE .RS 3 Searches the multimap 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() \fPis returned. .RE .br const_iterator .br find(const key_type& x) const; .RE .RS 3 Same as find above but returns a \f2const_iterator\fP. .RE .br iterator .br insert(const value_type& x); .br iterator .br insert(iterator position, const value_type& x); .RE .RS 3 \f2x\fP is inserted into the multimap. 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 \f2O(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 are inserted into the multimap. The iterators \f2first\fP and \f2last\fP must return values of type \f2pair\fP. This operation takes approximately \f2O(N*log(size()+N))\fP time. .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 multimap. .RE .br iterator .br lower_bound(const key_type& x); .RE .RS 3 Returns an \f2iterator\fP to the first multimap element whose key is greater than or equal to \f2x\fP. If no such element exists, then \f2end()\fP is returned. .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 multimap. .RE .br size_type .br size() const; .RE .RS 3 Returns the number of elements in the multimap. .RE .br void .br swap(multimap& x); .RE .RS 3 Swaps the contents of the multimap \f2x \fPwith the current multimap, \f2*this\fP. .RE .br iterator .br upper_bound(const key_type& x); .RE .RS 3 Returns an \f2iterator\fP to the first element whose key is less than or equal to \f2x\fP. If no such element exists, then \f2end()\fP is returned. .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 \f2value_types\fP (\f2key,value\fP pairs) using the comparison operation, \f2Compare\fP, of the current multimap. .RE .SH NON-MEMBER OPERATORS .br bool .br operator==(const multimap& x, .RE .RS 10 const multimap& 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==). \fPOtherwise it returns \f2false\fP. .RE .RE .RS 0 bool .br operator!=(const multimap& x, .RE .RS 10 const multimap& y); .RE .RS 3 Returns \f2!(x==y)\fP. .RE .RE .RS 0 bool .br operator<(const multimap& x, .RE .RS 10 const multimap& 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 bool .br operator>(const multimap& x, .RE .RS 10 const multimap& y); .RE .RS 3 Returns \f2y < x\fP. .RE .RE .RS 0 bool .br operator<=(const multimap& x, .RE .RS 10 const multimap& y); .RE .RS 3 Returns \f2!(y < x)\fP. .RE .RE .RS 0 bool .br operator>=(const multimap& x, .RE .RS 10 const multimap& y); .RE .RS 3 Returns \f2!(x < y)\fP. .RE .SH SPECIALIZED ALGORITHMS .RE .RS 0 template .br void swap(multimap& a, .RE .RS 10 multimap& b); .RE .RS 3 Swaps the contents of \f2a\fP and \f2b\fP. .RE .SH EXAMPLE .RE .RS 0 // .br // multimap.cpp .br // .RE .RS 1 #include .br #include .br #include .RE .RS 0 using namespace std; .br .br typedef multimap > 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.second << " has " << p.first << " days"; .br return out; .RE .RS 1 } .RE .RS 0 .RE .RS 1 // Print out a multimap .RE .RS 0 ostream& operator<<(ostream& out, months_type l) .RE .RS 1 { .RE .RS 2 copy(l.begin(),l.end(), ostream_iterator .RE .RS 13 (cout,"\\n")); .RE .RS 2 return out; .RE .RS 1 } .RE .RS 0 .br int main(void) .RE .RS 1 { .RE .RS 3 // create a multimap of months and the number of .br // days in the month .RE .RS 2 months_type months; .RE .RS 0 .RE .RS 2 typedef months_type::value_type value_type; .RE .RS 1 .RE .RS 3 // Put the months in the multimap .RE .RS 2 months.insert(value_type(31, string("January"))); .br months.insert(value_type(28, string("February"))); .br months.insert(value_type(31, string("March"))); .br months.insert(value_type(30, string("April"))); .br months.insert(value_type(31, string("May"))); .br months.insert(value_type(30, string("June"))); .br months.insert(value_type(31, string("July"))); .br months.insert(value_type(31, string("August"))); .br months.insert(value_type(30, string("September"))); .br months.insert(value_type(31, string("October"))); .br months.insert(value_type(30, string("November"))); .br months.insert(value_type(31, string("December"))); .RE .RS 0 .br .RE .RS 3 // print out the months .RE .RS 2 cout << "All months of the year" << endl << months .RE .RS 8 << endl; .RE .RS 0 .RE .RS 3 // Find the Months with 30 days .RE .RS 2 pair p = .RE .RS 9 months.equal_range(30); .RE .RS 0 .RE .RS 3 // print out the 30 day months .RE .RS 2 cout << endl << "Months with 30 days" << endl; .br copy(p.first,p.second, .RE .RS 7 ostream_iterator .br (cout,"\\n")); .RE .RS 1 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br All months of the year .br February has 28 days .br April has 30 days .br June has 30 days .br September has 30 days .br November has 30 days .br January has 31 days .br March has 31 days .br May has 31 days .br July has 31 days .br August has 31 days .br October has 31 days .br December has 31 days .br .br Months with 30 days .br April has 30 days .br June has 30 days .br September has 30 days .br November 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 multimap that takes two templatized iterators: .br template .br multimap (InputIterator, InputIterator, .RE .RS 10 const Compare& = Compare(), .br const Allocator& = Allocator()); .RE multimap also has an \f2insert\fP 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 multimap in the following two ways: .RE .RS 0 multimap::value_type intarray[10]; .br multimap first_map(intarry, intarray + 10); .br multimap second_multimap(first_multimap.begin(), .RE .RS 17 first_multimap.end()); .RE but not this way: .RE .RS 0 multimap .br long_multimap(first_multimap.begin(),first_multimap.end()); since the \f2long_multimap\fP and \f2first_multimap \fPare 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: \f2multimap, allocator >\fP instead of: \f2multimap\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, map