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

-

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

Return comparison object

Returns the comparison object associated with the container, which can be used to compare two elements of 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 container elements, and returns true if the first argument is considered to go before the second in the strict weak ordering the object defines, and false otherwise.

In set containers, the element values are the keys themselves, therefore key_comp and its sibling member function value_comp both return the same.

Parameters

none

Return value

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

Example

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

int main ()
{
  set<int> myset;
  set<int>::key_compare mycomp;
  set<int>::iterator it;
  int i,highest;

  mycomp = myset.key_comp();

  for (i=0; i<=5; i++) myset.insert(i);

  cout << "myset contains:";

  highest=*myset.rbegin();
  it=myset.begin();
  do {
    cout << " " << *it;
  } while ( mycomp(*it++,highest) );

  cout << endl;

  return 0;
}

Output:

myset contains: 0 1 2 3 4 5

Complexity

Constant.

See also

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

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us