是 std::pair顺序定义明确吗?
看来我可以对 std::vector
进行排序,它会根据 int 值进行排序。这是一个明确定义的事情吗?
std::pair
是否有基于其元素的默认排序?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
std::pair
使用字典比较:它将基于第一个元素进行比较。如果第一个元素的值相等,则将根据第二个元素进行比较。C++03 标准(第 20.2.2 节)中的定义是:
std::pair
uses lexicographic comparison: It will compare based on the first element. If the values of the first elements are equal, it will then compare based on the second element.The definition in the C++03 standard (section 20.2.2) is:
根据我的 C++0x 标准副本,第 20.3.3.26 节,
std::pair
定义了一个operator<
,对于两对 x 和 y,它返回我不确定这是否也是 2003 年标准的一部分。我还应该注意,如果元素本身不是 LessThanComparable,则不会编译。
According to my copy of the C++0x standard, section 20.3.3.26,
std::pair
has anoperator<
defined such that for two pairs x and y, it returnsI'm not certain if this is part of the 2003 standard as well. I should also note that this won't compile if the elements themselves are not LessThanComparable.
SGI 的文档
看起来它实际上是这两个元素的组合。
Documentation from SGI
Looks like it's actually a combination of both elements.
是的。
operator<()
是为std::pair
定义的,假设T1
和T2
> 本身具有可比性。Yes.
operator<()
is defined forstd::pair<T1, T2>
, assuming that bothT1
andT2
are themselves comparable.