为具有自定义键的 C++ 映射编写自定义比较器
Writing a custom comparator for a C++ map which has a custom defined key
我正在尝试为具有自定义键的 C++ 映射编写自定义比较器。
struct key { int year; int no; };
map<key, detail, compare> details_map;
如果 year
值相等,则必须比较 no
值。
我正在尝试找出一种方法来编写可以比较两个值的比较器。到目前为止,我只能编写一个比较器来比较一个值。
struct Compare{bool operator()(const key &lhs,const key &rhs)const{return lhs.year<rhs.year;}}
有人可以解释一下比较器在 map
中的工作原理吗?
另外,是否可以将比较器写成一个函数?
在您的 operator()
中,如果 year
值相等,只需比较 no
值:
struct Compare {
bool operator()(const key &lhs, const key &rhs) const {
if (lhs.year == rhs.year) {
return lhs.no < rhs.no;
}
return lhs.year < rhs.year;
}
};
是的,比较器可以作为独立函数实现:
bool Compare (const key &lhs, const key &rhs) {
if (lhs.year == rhs.year) {
return lhs.no < rhs.no;
}
return lhs.year < rhs.year;
}
或者,您可以让比较器使用 std::tie()
来比较您的关键字段。参见 。
不过,为您的 key
类型实现 operator<
会更有意义:
struct key {
int year;
int no;
bool operator<(const key &rhs) const {
if (year == rhs.year) {
return no < rhs.no;
}
return year < rhs.year;
}
};
或
struct key {
int year;
int no;
};
bool operator<(const key &lhs, const key &rhs) {
if (lhs.year == rhs.year) {
return lhs.no < rhs.no;
}
return lhs.year < rhs.year;
}
那么你不需要单独的比较器:
map<key, detail> details_map;
std::tie
允许简单的字典序比较:
struct Compare {
bool operator()(const key& lhs, const key& rhs) const {
return std::tie(lhs.year, lhs.no) < std::tie(rhs.year, rhs.no);
}
};
Method/function as_tuple
避免一些重复可能很有趣:
struct key { int year; int no; };
auto as_tuple(const key& k) { return std::tie(k.year, k.no); }
struct Compare {
bool operator()(const key& lhs, const key& rhs) const {
return as_tuple(lhs) < as_tuple(rhs);
}
};
我正在尝试为具有自定义键的 C++ 映射编写自定义比较器。
struct key { int year; int no; };
map<key, detail, compare> details_map;
如果 year
值相等,则必须比较 no
值。
我正在尝试找出一种方法来编写可以比较两个值的比较器。到目前为止,我只能编写一个比较器来比较一个值。
struct Compare{bool operator()(const key &lhs,const key &rhs)const{return lhs.year<rhs.year;}}
有人可以解释一下比较器在 map
中的工作原理吗?
另外,是否可以将比较器写成一个函数?
在您的 operator()
中,如果 year
值相等,只需比较 no
值:
struct Compare {
bool operator()(const key &lhs, const key &rhs) const {
if (lhs.year == rhs.year) {
return lhs.no < rhs.no;
}
return lhs.year < rhs.year;
}
};
是的,比较器可以作为独立函数实现:
bool Compare (const key &lhs, const key &rhs) {
if (lhs.year == rhs.year) {
return lhs.no < rhs.no;
}
return lhs.year < rhs.year;
}
或者,您可以让比较器使用 std::tie()
来比较您的关键字段。参见
不过,为您的 key
类型实现 operator<
会更有意义:
struct key {
int year;
int no;
bool operator<(const key &rhs) const {
if (year == rhs.year) {
return no < rhs.no;
}
return year < rhs.year;
}
};
或
struct key {
int year;
int no;
};
bool operator<(const key &lhs, const key &rhs) {
if (lhs.year == rhs.year) {
return lhs.no < rhs.no;
}
return lhs.year < rhs.year;
}
那么你不需要单独的比较器:
map<key, detail> details_map;
std::tie
允许简单的字典序比较:
struct Compare {
bool operator()(const key& lhs, const key& rhs) const {
return std::tie(lhs.year, lhs.no) < std::tie(rhs.year, rhs.no);
}
};
Method/function as_tuple
避免一些重复可能很有趣:
struct key { int year; int no; };
auto as_tuple(const key& k) { return std::tie(k.year, k.no); }
struct Compare {
bool operator()(const key& lhs, const key& rhs) const {
return as_tuple(lhs) < as_tuple(rhs);
}
};