std::set<Key,Compare,Allocator>::value_comp
来自cppreference.com
std::set::value_compare value_comp()const; | ||
返回比较值的函数对象。它与 key_comp 相同。
目录 |
[编辑]参数
(无)
[编辑]返回值
比较值的函数对象。
[编辑]复杂度
常数。
[编辑]示例
运行此代码
#include <iostream>#include <set>#include <utility> // 演示模 97 的键比较函数struct ModCmp {bool operator()(int lhs, int rhs)const{return(lhs %97)<(rhs %97);}}; int main(){std::set<int, ModCmp> cont{1, 2, 3, 4, 5}; // 行为与 key_comp() 相同auto comp_func = cont.value_comp(); for(constint val{100};constint key : cont){constbool before = comp_func(key, val);constbool after = comp_func(val, key); std::cout<<"键 ("<< key <<") ";if(!before &&!after)std::cout<< key <<" 等价于键 ("<< val <<")\n";elseif(before)std::cout<< key <<" 在键 ("<< val <<") 之前\n";elseif(after)std::cout<< key <<" 在键 ("<< val <<") 之后\n";elsestd::unreachable();}}
输出:
键 (1) 在键 (100) 之前 键 (2) 在键 (100) 之前 键 (3) 等价于键 (100) 键 (4) 在键 (100) 之后 键 (5) 在键 (100) 之后
[编辑]参阅
返回用于比较键的函数 (公开成员函数) |