public member function
<set>

std::set::key_comp

key_compare key_comp() const;
Return comparison object
Returns a copy of the comparison object used by the container.

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

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

In set containers, the keys to sort the elements are the values themselves, thereforekey_compand its sibling member function value_comp are equivalent.

引數



返回值

The comparison object.
成員型別key_compareis the type of the comparison object associated to the container, defined in set as an alias of its second 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
// set::key_comp
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  int highest;

  std::set<int>::key_compare mycomp = myset.key_comp();

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

  std::cout << "myset contains:";

  highest=*myset.rbegin();
  std::set<int>::iterator it=myset.begin();
  do {
    std::cout << ' ' << *it;
  } while ( mycomp(*(++it),highest) );

  std::cout << '\n';

  return 0;
}

輸出
myset contains: 0 1 2 3 4


複雜度

常量。

迭代器有效性

沒有變化。

資料競爭

訪問容器。
併發訪問set的元素是安全的。

異常安全

強保證:如果丟擲異常,容器沒有發生變化。

另見