我正在尝试为具有自定义键的C++映射编写自定义比较器.
struct key { int year; int no; };
map<key, detail, compare> details_map;
Run Code Online (Sandbox Code Playgroud)
如果year
值相等,则必须比较no
值.
我试图想出一种方法来编写一个可以比较两个值的比较器.到目前为止,我只能写一个比较一个值的比较器.
struct Compare{bool operator()(const key &lhs,const key &rhs)const{return lhs.year<rhs.year;}}
Run Code Online (Sandbox Code Playgroud)
有人可以解释比较器如何工作map
?
此外,是否可以将比较器写为函数?