C++ Set 库 - key_comp 函数
描述
它返回容器使用的比较对象的副本。
声明
以下是 std::set::key_comp 在各种 C++ 版本中的工作方式。
C++98
key_compare key_comp() const;
C++11
key_compare key_comp() const;
返回值
它返回容器使用的比较对象的副本。
异常
如果抛出异常,则容器中没有任何变化。
时间复杂度
时间复杂度取决于对数。
示例
以下示例显示了 std::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 <= 10; 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 5 6 7 8 9

