A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/map/map/key_comp/ below:

public member function

<map>

std::map::key_comp
key_compare key_comp() const;

Return key comparison object

Returns a copy of the comparison object used by the container to compare keys.

The comparison object of a map object is set on construction. Its type (member key_compare) is the third template parameter of the map template. By default, this is a less object, which returns the same as operator<.

This object determines the order of the elements in the container: it is a function pointer or a function object that 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 it defines, and false otherwise.

Two keys are considered equivalent if key_comp returns false reflexively (i.e., no matter the order in which the keys are passed as arguments).



Parameters none

Return value The comparison object.
Member type key_compare is the type of the comparison object associated to the container, defined in map as an alias of its third template parameter (Compare).

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// map::key_comp
#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;

  std::map<char,int>::key_compare mycomp = mymap.key_comp();

  mymap['a']=100;
  mymap['b']=200;
  mymap['c']=300;

  std::cout << "mymap contains:\n";

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

  std::map<char,int>::iterator it = mymap.begin();
  do {
    std::cout << it->first << " => " << it->second << '\n';
  } while ( mycomp((*it++).first, highest) );

  std::cout << '\n';

  return 0;
}

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


Complexity Constant.

Iterator validity No changes.

Data races The container is accessed.
No contained elements are accessed: concurrently accessing or modifying them is safe.

Exception safetyStrong guarantee: if an exception is thrown, there are no changes in the container.

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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4