public member function
<map>

std::multimap::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.

By default, this is a less object, which returns the same asoperator<.

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 returnstrueif the first argument is considered to go before the second in the strict weak ordering it defines, andfalse否则为 false。

Two keys are considered equivalent ifkey_comp返回 11。falsereflexively (i.e., no matter the order in which the keys are passed as arguments).

参数



返回值

The comparison object.
成员类型key_compareis the type of the comparison object associated to the container, defined in multimap as an alias of its third template parameter (Compare).

示例

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
28
// multimap::key_comp
#include <iostream>
#include <map>

int main ()
{
  std::multimap<char,int> mymultimap;

  std::multimap<char,int>::key_compare mycomp = mymultimap.key_comp();

  mymultimap.insert (std::make_pair('a',100));
  mymultimap.insert (std::make_pair('b',200));
  mymultimap.insert (std::make_pair('b',211));
  mymultimap.insert (std::make_pair('c',300));

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

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

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

  std::cout << '\n';

  return 0;
}

输出
mymultimap contains:
a => 100
b => 200
b => 211
c => 300


复杂度

常量。

迭代器有效性

没有变化。

数据竞争

访问容器。
No contained elements are accessed: concurrently accessing or modifying them is safe.

异常安全

强保证:如果抛出异常,容器没有发生变化。

另见