为什么 std::map 重载运算符 < 不使用比较
Why std::map overloaded operator < does not use the Compare
来自 http://www.cplusplus.com/reference/map/map/operators/ 我注意到:
"Notice that none of these operations take into consideration the internal comparison object of either container, but compare the elements (of type value_type) directly."
这就是说重载运算符“<”在其声明中没有使用 Compare
(参考http://www.cplusplus.com/reference/map/map/)
std::map
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
其中 Compare
是
Compare: A binary predicate that takes two element keys as arguments
and returns a bool
. The expression comp(a,b)
, where comp
is an object
of this type and a
and b
are key values, shall return true if a
is
considered to go before b
in the strict weak ordering the function
defines. The map object uses this expression to determine both the
order the elements follow in the container and whether two element
keys are equivalent (by comparing them reflexively: they are
equivalent if !comp(a,b) && !comp(b,a))
. No two elements in a map
container can have equivalent keys. This can be a function pointer or
a function object (see constructor for an example). This defaults to
less<T>
, which returns the same as applying the less-than operator
(a<b)
. Aliased as member type map::key_compare
.
我不太明白,为什么不在“<”运算符中使用Compare
呢?
Compare
用于比较key_type
。地图的 <
运算符实际上是在比较 mapped_type
value_type
,而不是 key_type
,因此 Compare
不适用。 value_type
是一对 key_type
和 mapped_type
.
不过老实说,我认为首先给 map 一个 operator<
是运算符重载过度的情况。说一张地图是 "less than" 另一张地图是什么意思并不是很明显。如果您想要 lexigraphical_compare
,我建议您直接调用它,或者至少在您的代码中记录地图比较的含义。
Why std::map
overloaded operator <
does not use the Compare
一个很好的理由是确保行为有意义并不容易。鉴于 Compare
仿函数可以是有状态的,相同类型的两个映射可能具有完全不同的排序标准。例如,
struct Cmp
{
Comp(bool flip) : flip(flip) {}
bool operator()(int lhs, int rhs) const
{
return flip ? lhs < rhs : rhs < lhs;
}
bool flip;
};
这两张地图类型相同,但顺序不同:
std::map<int, std::string, Cmp> m0(Cmp(false));
std::map<int, std::string, Cmp> m1(Cmp(true));
bool b = m0 < m1; // which Cmp should this use?
这不一定是使用 <
等的理由,而是不使用 Compare
.
的充分理由
来自 http://www.cplusplus.com/reference/map/map/operators/ 我注意到:
"Notice that none of these operations take into consideration the internal comparison object of either container, but compare the elements (of type value_type) directly."
这就是说重载运算符“<”在其声明中没有使用 Compare
(参考http://www.cplusplus.com/reference/map/map/)
std::map
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
其中 Compare
是
Compare: A binary predicate that takes two element keys as arguments and returns a
bool
. The expressioncomp(a,b)
, wherecomp
is an object of this type anda
andb
are key values, shall return true ifa
is considered to go beforeb
in the strict weak ordering the function defines. The map object uses this expression to determine both the order the elements follow in the container and whether two element keys are equivalent (by comparing them reflexively: they are equivalent if!comp(a,b) && !comp(b,a))
. No two elements in a map container can have equivalent keys. This can be a function pointer or a function object (see constructor for an example). This defaults toless<T>
, which returns the same as applying the less-than operator(a<b)
. Aliased as member typemap::key_compare
.
我不太明白,为什么不在“<”运算符中使用Compare
呢?
Compare
用于比较key_type
。地图的 <
运算符实际上是在比较 mapped_type
value_type
,而不是 key_type
,因此 Compare
不适用。 value_type
是一对 key_type
和 mapped_type
.
不过老实说,我认为首先给 map 一个 operator<
是运算符重载过度的情况。说一张地图是 "less than" 另一张地图是什么意思并不是很明显。如果您想要 lexigraphical_compare
,我建议您直接调用它,或者至少在您的代码中记录地图比较的含义。
Why
std::map
overloadedoperator <
does not use theCompare
一个很好的理由是确保行为有意义并不容易。鉴于 Compare
仿函数可以是有状态的,相同类型的两个映射可能具有完全不同的排序标准。例如,
struct Cmp
{
Comp(bool flip) : flip(flip) {}
bool operator()(int lhs, int rhs) const
{
return flip ? lhs < rhs : rhs < lhs;
}
bool flip;
};
这两张地图类型相同,但顺序不同:
std::map<int, std::string, Cmp> m0(Cmp(false));
std::map<int, std::string, Cmp> m1(Cmp(true));
bool b = m0 < m1; // which Cmp should this use?
这不一定是使用 <
等的理由,而是不使用 Compare
.