multimap(3C++) - multimap(3C++)
Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.
NAMEmultimap
- 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.
SYNOPSIS
#include <map>
template <class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key, T>> >
class multimap;
DESCRIPTION
multimap_<Key_,T,_Compare,_Allocator> gives fast access to stored val‐
ues of type T that are indexed by keys of type Key. The default opera‐
tion for key comparison is the < operator. Unlike map, multimap allows
insertion of duplicate keys.
multimap uses bidirectional iterators that point to an instance of
pair<const Key x, T y> where x is the key and y is the stored value
associated with that key. The definition of multimap includes a type‐
def to this pair called value_type.
The types used for both the template parameters Key and T must include
the following (where T is the type, t is a value of T and u is a const
value of T):
Copy constructors T(t) and T(u)
Destructor t.~T()
Address of &t and &u yielding T* and const T* respectively
Assignment t = a where a is a (possibly const) value of T
The type used for the Compare template parameter must satisfy the
requirements for binary functions.
INTERFACE
template <class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key, T>> >
class multimap{
public:
// types
typedef Key key_type;
typedef T mapped_type;
typedef pair<const Key, T> value_type;
typedef Compare key_compare;
typedef Allocator allocator_type;
typedef typename
Allocator::reference reference;
typedef typename
Allocator::const_reference const_reference;
class iterator;
class const_iterator;
typedef typename
Allocator::size_type size_type;
typedef typename
Allocator::difference_type difference_type;
typedef typename std::reverse_iterator<iterator>
reverse_iterator;
typedef typename std::reverse_iterator<const_iterator>
const_reverse_iterator;
class value_compare
: public binary_function<value_type, value_type, bool>
{
friend class multimap<Key, T, Compare, Allocator>;
protected :
Compare comp;
value_compare (Compare C) : comp(c) {}
public :
bool operator() (const value_type&,
const value_type&) const;
};
// Construct/Copy/Destroy
explicit multimap (const Compare& = Compare(),
const Allocator& =
Allocator());
template <class InputIterator>
multimap (InputIterator, InputIterator,
const Compare& = Compare(),
const Allocator& = Allocator());
multimap (const multimap<Key, T, Compare, Allocator>&);
~multimap ();
multimap<Key, T, Compare, Allocator>& operator=
(const multimap<Key, T, Compare, Allocator>&);
allocator_type get_allocator () const;
// Iterators
iterator begin ();
const_iterator begin () const;
iterator end ();
const_iterator end () const;
reverse_iterator rbegin ();
const_reverse_iterator rbegin () const;
reverse_iterator rend ();
const_reverse_iterator rend () const;
// Capacity
bool empty () const;
size_type size () const;
size_type max_size () const;
// Modifiers
iterator insert (const value_type&);
iterator insert (iterator, const value_type&);
template <class InputIterator>
void insert (InputIterator, InputIterator);
void erase (iterator);
size_type erase (const key_type&);
void erase (iterator, iterator);
void swap (multimap<Key, T, Compare, Allocator>&);
void clear ();
// Observers
key_compare key_comp () const;
value_compare value_comp () const;
// Multimap operations
iterator find (const key_type&);
const_iterator find (const key_type&) const;
size_type count (const key_type&) const;
iterator lower_bound (const key_type&);
const_iterator lower_bound (const key_type&) const;
iterator upper_bound (const key_type&);
const_iterator upper_bound (const key_type&) const;
pair<iterator, iterator> equal_range (const key_type&);
pair<const_iterator, const_iterator>
equal_range (const key_type&) const;
};
// Non-member Operators
template <class Key, class T, class Compare,
class Allocator>
bool operator== (const multimap<Key, T, Compare,
Allocator>&,
const multimap<Key, T, Compare,
Allocator>&);
template <class Key, class T, class Compare,
class Allocator>
bool operator!= (const multimap<Key, T, Compare,
Allocator>&,
const multimap<Key, T, Compare,
Allocator>&);
template <class Key, class T, class Compare,
class Allocator>
bool operator< (const multimap<Key, T, Compare,
Allocator>&,
const multimap<Key, T, Compare,
Allocator>&);
template <class Key, class T, class Compare,
class Allocator>
bool operator> (const multimap<Key, T, Compare,
Allocator>&,
const multimap<Key, T, Compare,
Allocator>&);
template <class Key, class T, class Compare,
class Allocator>
bool operator<= (const multimap<Key, T, Compare,
Allocator>&,
const multimap<Key, T, Compare,
Allocator>&);
template <class Key, class T, class Compare,
class Allocator>
bool operator>= (const multimap<Key, T, Compare,
Allocator>&,
const multimap<Key, T, Compare,
Allocator>&);
// Specialized Algorithms
template <class Key, class T, class Compare,
class Allocator>
void swap (multimap<Key, T, Compare, Allocator>&,
multimap<Key, T, Compare, Allocator>&;
CONSTRUCTORSexplicit multimap(const Compare& comp = Compare(),
const Allocator& alloc = Allocator());
Constructs an empty multimap that uses the optional relation comp to order
keys and the allocator alloc for all storage management.
template <class InputIterator>
multimap(InputIterator first,
InputIterator last,
const Compare& comp = Compare()
const Allocator& alloc = Allocator());
Constructs a multimap containing values in the range [first, last). Cre‐
ation of the new multimap is only guaranteed to succeed if the iterators
first and last return values of type pair<class Key, class T>.
multimap(const multimap<Key, T, Compare, Allocator>& x);
Creates a new multimap by copying all pairs of key and value from x.
DESTRUCTORS
~multimap();
Releases any allocated memory for this multimap.
ASSIGNMENT OPERATORS
multimap<Key, T, Compare, Allocator>&
operator=(const multimap<Key, T, Compare, Allocator>& x);
Replaces the contents of *this with a copy of the multimap x.
ALLOCATORS
allocator_type
get_allocator() const;
Returns a copy of the allocator used by self for storage management.
ITERATORS
iterator
begin();
Returns a bidirectional iterator pointing to the first element stored in
the multimap. "First" is defined by the multimap's comparison operator,
Compare.
const_iteratorbegin() const;
Returns a const_iterator pointing to the first element stored in the mul‐
timap. "First" is defined by the multimap's comparison operator, Compare.
iteratorend();
Returns a bidirectional iterator pointing to the last element stored in the
multimap (in other words, the off-the-end value).
const_iteratorend() const;
Returns a const_iterator pointing to the last element stored in the mul‐
timap.
reverse_iteratorrbegin();
Returns a reverse_iterator pointing to the first element stored in the mul‐
timap. "First" is defined by the multimap's comparison operator, Compare.
const_reverse_iteratorrbegin() const;
Returns a const_reverse_iterator pointing to the first element stored in
the multimap.
reverse_iteratorrend();
Returns a reverse_iterator pointing to the last element stored in the mul‐
timap (in other words, the off-the-end value).
const_reverse_iteratorrend() const;
Returns a const_reverse_iterator pointing to the last element stored in the
multimap.
MEMBER FUNCTIONS
void
clear();
Erases all elements from the self.
size_typecount(const key_type& x) const;
Returns the number of elements in the multimap with the key value x.
boolempty() const;
Returns true if the multimap is empty, false otherwise.
pair<iterator,iterator>
equal_range(const key_type& x);
pair<const_iterator,const_iterator>
equal_range(const key_type& x) const;
Returns the pair (lower_bound(x), upper_bound(x)).
voiderase(iterator first, iterator last);
If the iterators first and last point to the same multimap and last is
reachable from first, all elements in the range (first, last) are deleted
from the multimap. Returns an iterator pointing to the element following
the last deleted element or end(), if there were no elements after the
deleted range.
voiderase(iterator position);
Deletes the multimap element pointed to by the iterator position. Returns
an iterator pointing to the element following the deleted element, or
end(), if the deleted item was the last one in this list.
size_typeerase(const key_type& x);
Deletes the elements with the key value x from the map, if any exist.
Returns the number of deleted elements, or 0 otherwise.
iteratorfind(const key_type& x);
Searches the multimap for a pair with the key value x and returns an itera‐
tor to that pair if it is found. If such a pair is not found the value
end() is returned.
const_iteratorfind(const key_type& x) const;
Same as find above but returns a const_iterator.
iteratorinsert(const value_type& x);
iteratorinsert(iterator position, const value_type& x);
x 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
position, then it takes amortized constant time. Otherwise it takes O(log
N) time.
template <class InputIterator>
voidinsert(InputIterator first, InputIterator last);
Copies of each element in the range [first, last) are inserted into the
multimap. The iterators first and last must return values of type
pair<T1,T2>. This operation takes approximately O(N*log(size()+N)) time.
key_comparekey_comp() const;
Returns a function object capable of comparing key values using the compar‐
ison operation, Compare, of the current multimap.
iteratorlower_bound(const key_type& x);
Returns an iterator to the first multimap element whose key is greater than
or equal to x. If no such element exists, then end() is returned.
const_iteratorlower_bound(const key_type& x) const;
Same as lower_bound above but returns a const_iterator.
size_typemax_size() const;
Returns the maximum possible size of the multimap.
size_typesize() const;
Returns the number of elements in the multimap.
voidswap(multimap<Key, T, Compare, Allocator>& x);
Swaps the contents of the multimap x with the current multimap, *this.
iteratorupper_bound(const key_type& x);
Returns an iterator to the first element whose key is less than or equal to
x. If no such element exists, then end() is returned.
const_iteratorupper_bound(const key_type& x) const;
Same as upper_bound above but returns a const_iterator.
value_comparevalue_comp() const;
Returns a function object capable of comparing value_types (key,value
pairs) using the comparison operation, Compare, of the current multimap.
NON-MEMBER OPERATORS
bool
operator==(const multimap<Key, T, Compare, Allocator>& x,
const multimap<Key, T, Compare, Allocator>& y);
Returns true if all elements in x are element-wise equal to all elements in
y, using (T::operator==). Otherwise it returns false.
booloperator!=(const multimap<Key, T, Compare, Allocator>& x,
const multimap<Key, T, Compare, Allocator>& y);
Returns !(x==y).
booloperator<(const multimap<Key, T, Compare, Allocator>& x,
const multimap<Key, T, Compare, Allocator>& y);
Returns true if x is lexicographically less than y. Otherwise, it returns
false.
booloperator>(const multimap<Key, T, Compare, Allocator>& x,
const multimap<Key, T, Compare, Allocator>& y);
Returns y < x.
booloperator<=(const multimap<Key, T, Compare, Allocator>& x,
const multimap<Key, T, Compare, Allocator>& y);
Returns !(y < x).
booloperator>=(const multimap<Key, T, Compare, Allocator>& x,
const multimap<Key, T, Compare, Allocator>& y);
Returns !(x < y).
SPECIALIZED ALGORITHMStemplate<class Key, class T, class Compare, class Allocator>
void swap(multimap<Key, T, Compare, Allocator>& a,
multimap<Key, T, Compare, Allocator>& b);
Swaps the contents of a and b.
EXAMPLE
//
// multimap.cpp
//
#include <string>
#include <map>
#include <iostream>
using namespace std;
typedef multimap<int, string, less<int> > months_type;
// Print out a pair
template <class First, class Second>
ostream& operator<<(ostream& out,
const pair<First,Second>& p)
{
cout << p.second << " has " << p.first << " days";
return out;
}
// Print out a multimapostream& operator<<(ostream& out, months_type l)
{
copy(l.begin(),l.end(), ostream_iterator
<months_type::value_type,char>(cout,"\n"));
return out;
}
int main(void)
{
// create a multimap of months and the number of
// days in the month
months_type months;
typedef months_type::value_type value_type;
// Put the months in the multimap
months.insert(value_type(31, string("January")));
months.insert(value_type(28, string("February")));
months.insert(value_type(31, string("March")));
months.insert(value_type(30, string("April")));
months.insert(value_type(31, string("May")));
months.insert(value_type(30, string("June")));
months.insert(value_type(31, string("July")));
months.insert(value_type(31, string("August")));
months.insert(value_type(30, string("September")));
months.insert(value_type(31, string("October")));
months.insert(value_type(30, string("November")));
months.insert(value_type(31, string("December")));
// print out the months
cout << "All months of the year" << endl << months
<< endl;
// Find the Months with 30 days
pair<months_type::iterator,months_type::iterator> p =
months.equal_range(30);
// print out the 30 day months
cout << endl << "Months with 30 days" << endl;
copy(p.first,p.second,
ostream_iterator<months_type::value_type,char>
(cout,"\n"));
return 0;
}
Program OutputAll months of the yearFebruary has 28 daysApril has 30 daysJune has 30 daysSeptember has 30 daysNovember has 30 daysJanuary has 31 daysMarch has 31 daysMay has 31 daysJuly has 31 daysAugust has 31 daysOctober has 31 daysDecember has 31 daysMonths with 30 daysApril has 30 daysJune has 30 daysSeptember has 30 daysNovember has 30 daysWARNINGS
Member function templates are used in all containers included in the
Standard Template Library. An example of this feature is the construc‐
tor for multimap<Key,T,Compare,Allocator> that takes two templatized
iterators:
template <class InputIterator>
multimap (InputIterator, InputIterator,
const Compare& = Compare(),
const Allocator& = Allocator());
multimap also has an insert function of this type. These functions, when not
restricted by compiler limitations, allow you to use any type of input itera‐
tor as arguments. For compilers that do not support this feature, substitute
functions allow you to use an iterator obtained from the same type of con‐
tainer 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:multimap<int,int>::value_type intarray[10];
multimap<int,int> first_map(intarry, intarray + 10);
multimap<int,int> second_multimap(first_multimap.begin(),
first_multimap.end());
but not this way:multimap<long,long>
long_multimap(first_multimap.begin(),first_multimap.end());
since the long_multimap and first_multimap are not the same type.Also, many compilers do not support default template arguments. If your com‐
piler is one of these you always need to supply the Compare template argumentand the Allocator template argument. For instance, you have to write:
multimap<int, int, less<int>, allocator<int> >
instead of:multimap<int, int>
If your compiler does not support namespaces, then you do not need the using
declaration for std.SEE ALSO
allocator, Containers, Iterators, map
Rogue Wave Software 02 Apr 1998 multimap(3C++)