cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : multimap : key_comp
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
multimap
comparison operators
multimap::multimap
multimap::~multimap
member functions:
· multimap::begin
· multimap::clear
· multimap::count
· multimap::empty
· multimap::end
· multimap::equal_range
· multimap::erase
· multimap::find
· multimap::get_allocator
· multimap::insert
· multimap::key_comp
· multimap::lower_bound
· multimap::max_size
· multimap::operator=
· multimap::rbegin
· multimap::rend
· multimap::size
· multimap::swap
· multimap::upper_bound
· multimap::value_comp

-

multimap::key_comp public member function
key_compare key_comp ( ) const;

Return key comparison object

Returns the comparison object associated with the container, which can be used to compare the keys of two elements in the container.

This comparison object is set on object construction, and may either be a pointer to a function or an object of a class with a function call operator. In both cases it takes two arguments of the same type as the element keys, and returns true if the first argument is considered to go before the second in the strict weak ordering for the multimap object, and false otherwise.

Notice that the returned comparison object takes as parameters the element keys themselves, not entire element values (pairs) as referenced by a multimap iterator. To compare the keys of elements using dereferenced iterators, the member function multimap::value_comp may better suit your needs.

Parameters

none

Return value

The key comparison object.
multimap::key_compare is a member type defined to Compare, which is the third template parameter in the multimap class template.

Example

// multimap::key_comp
#include <iostream>
#include <map>
using namespace std;

int main ()
{
  multimap<char,int> mymultimap;
  multimap<char,int>::key_compare mycomp;
  multimap<char,int>::iterator it;
  char highest;

  mycomp = mymultimap.key_comp();

  mymultimap.insert (pair<char,int>('a',100));
  mymultimap.insert (pair<char,int>('b',200));
  mymultimap.insert (pair<char,int>('b',211));
  mymultimap.insert (pair<char,int>('c',300));

  cout << "mymultimap contains:\n";

  highest=mymultimap.rbegin()->first;     // key value of last element

  it=mymultimap.begin();
  do {
    cout << (*it).first << " => " << (*it).second << endl;
  } while ( mycomp((*it++).first, highest) );

  cout << endl;

  return 0;
}

Output:

mymap contains:
a => 100
b => 200
b => 211
c => 300

Complexity

Constant.

See also

multimap::value_comp Return value comparison object (public member function)
multimap::find Get iterator to element (public member function)
multimap::count Count elements with a specific key (public member function)
multimap::lower_bound Return iterator to lower bound (public member function)
multimap::upper_bound Return iterator to upper bound (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us