Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

map


Container

Summary

An associative container with access to non-key values using unique keys. A map supports bidirectional iterators.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <map>
template <class Key, class T, class Compare = less<Key> 
          class Allocator = allocator<pair<const Key, T>> >
class map;

Description

map <Key, T, Compare, Allocator> gives fast access to stored values of type T that are indexed by unique keys of type Key. The default operation for key comparison is the < operator.

map has 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 map includes a typedef 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 map {

public:

// types

   typedef Key key_type;
   typedef typename Allocator::pointer pointer;
   typedef typename Allocator::const_pointer const_pointer;
   typedef T mapped_type;
   typedef pair<const Key, T> value_type;
   typedef Compare key_compare;
   typedef Allocator allocator_type;
   class iterator;
   class const_iterator;
   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 map<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 map (const Compare& = Compare(),
                 const Allocator& = Allocator ());
   template <class InputIterator>
    map (InputIterator, InputIterator,
         const Compare& = Compare(),
         const Allocator& = Allocator ());
   map (const map<Key, T, Compare, Allocator>&);
   ~map();
   map<Key, T, Compare, Allocator>& 
    operator= (const map<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;

// Element Access

   mapped_type& operator[] (const key_type&);

// Modifiers

   pair<iterator, bool> 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 (map<Key, T, Compare, Allocator>&);
   void clear();

// Observers

   key_compare key_comp() const;
   value_compare value_comp() const;

// Map operations

   iterator find (const key_value&);
   const_iterator find (const key_value&) 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 Map Operators

template <class Key, class T, class Compare, class Allocator>
 bool operator== (const map<Key, T, Compare, Allocator>&,
                  const map<Key, T, Compare, Allocator>&);

template <class Key, class T, class Compare, class Allocator>
 bool operator!= (const map<Key, T, Compare, Allocator>&,
                  const map<Key, T, Compare, Allocator>&);

template <class Key, class T, class Compare, class Allocator>
 bool operator< (const map<Key, T, Compare, Allocator>&,
                 const map<Key, T, Compare, Allocator>&);

template <class Key, class T, class Compare, class Allocator>
 bool operator> (const map<Key, T, Compare, Allocator>&,
                 const map<Key, T, Compare, Allocator>&);

template <class Key, class T, class Compare, class Allocator>
 bool operator<= (const map<Key, T, Compare, Allocator>&,
                 const map<Key, T, Compare, Allocator>&);

template <class Key, class T, class Compare, class Allocator>
 bool operator>= (const map<Key, T, Compare, Allocator>&,
                 const map<Key, T, Compare, Allocator>&);


// Specialized Algorithms

template <class Key, class T, class Compare, class Allocator>
 void swap (map<*Key,T,Compare,Allocator>&,
            map<Key,T,Compare,Allocator>&);

Constructors

explicit map(const Compare& comp = Compare(),
             const Allocator& alloc = Allocator());
template <class InputIterator>
map(InputIterator first, InputIterator last,
    const Compare& comp = Compare(),
    const Allocator& alloc = Allocator());
map(const map<Key,T,Compare,Allocator>& x);

Destructors

~map();

Allocators

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;

Member Operators

map<Key, T, Compare, Allocator>& 
operator=(const map<Key, T, Compare, Allocator>& x);
mapped_type& 
operator[](const key_type& x);

Member Functions

void
clear();
size_type 
count(const key_type& x) const;
bool 
empty() const;
pair<iterator, iterator> 
equal_range (const key_type& x);
pair<const_iterator,const_iterator> 
equal_range (const key_type& x) const;
void
erase(iterator position);
void
erase(iterator first, iterator last);
size_type 
erase(const key_type& x);
iterator 
find(const key_type& x);
const_iterator find(const key_type& x) const; 
pair<iterator, bool> 
insert(const value_type& x);
iterator 
insert(iterator position, const value_type& x);
template <class InputIterator>
void 
insert(InputIterator first, InputIterator last);
key_compare 
key_comp() const;
iterator 
lower_bound(const key_type& x);
const_iterator 
lower_bound(const key_type& x) const;
size_type 
max_size() const;
size_type 
size() const;
void 
swap(map<Key, T, Compare, Allocator>& x);
iterator 
upper_bound(const key_type& x);
const_iterator 
upper_bound(const key_type& x) const;
value_compare 
value_comp() const;

Non-member Operators

template <class Key, class T, class Compare, class Allocator>
bool operator==(const map<Key, T, Compare, Allocator>& x,
                 const map<Key, T, Compare, Allocator>& y);
template <class Key, class T, class Compare, class Allocator>
bool operator!=(const map<Key, T, Compare, Allocator>& x,
                 const map<Key, T, Compare, Allocator>& y);
template <class Key, class T, class Compare, class Allocator>
bool operator<(const map<Key, T, Compare, Allocator>& x,
                const map<Key, T, Compare, Allocator>& y);
template <class Key, class T, class Compare, class Allocator>
bool operator>(const map<Key, T, Compare, Allocator>& x,
                const map<Key, T, Compare, Allocator>& y);
template <class Key, class T, class Compare, class Allocator>
bool operator<=(const map<Key, T, Compare, Allocator>& x,
                const map<Key, T, Compare, Allocator>& y);
template <class Key, class T, class Compare, class Allocator>
bool operator>=(const map<Key, T, Compare, Allocator>& x,
                const map<Key, T, Compare, Allocator>& y);

Specialized Algorithms

template <class Key, class T, class Compare, class Allocator>
void swap(map<Key, T, Compare, Allocator>& a,
           map<Key, T, Compare, Allocator>& b);

Example

Program Output

Warnings

Member function templates are used in all containers included in the Standard Template Library. An example of this feature is the constructor for map<Key,T,Compare,Allocator> that takes two templatized iterators:

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:

But not this way:

Since the long_map and first_map 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 Compare template argument and the Allocator template argument. For instance, you have to write:

map<int, int, less<int>, allocator<int> >

instead of:

map<int, int>

If your compiler does not support namespaces, then you do not need the using declaration for std.

See Also

allocator, Containers, Iterators, multimap



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998