简体   繁体   English

插入 std::set 作为 std::map 的键

[英]Inserting std::set as keys to std::map

I am using std::set<std::uint32_t> as keys to a std::map .我使用std::set<std::uint32_t>作为std::map键。 I have read on cppreference that std::set implements the Compare operation via std::lexicographical_compare of two std::set - what does this actually mean?我在cppreference上读到std::set通过两个std::set std::lexicographical_compare实现比较操作 - 这实际上意味着什么?

Also, is it possible for two std::set with the same std::uint32_t s inserted, but in different order, to not evaluate equal?另外,两个std::set是否有可能插入相同的std::uint32_t ,但顺序不同,评估结果不相等?

It means what it says - comparing two sets does so lexicographically .这意味着它所说的 - 比较两个集合是按字典顺序进行的

The very same site tells you what that means if you simply click through:同一个网站会告诉您如果您只需点击一下这意味着什么

Lexicographical comparison is a operation with the following properties:字典比较是具有以下属性的操作:

  • Two ranges are compared element by element.两个范围逐个元素地进行比较。
  • The first mismatching element defines which range is lexicographically less or greater than the other.第一个不匹配元素定义哪个范围在字典上比另一个范围小或大。
  • If one range is a prefix of another, the shorter range is lexicographically less than the other.如果一个范围是另一个范围的前缀,则较短的范围按字典顺序小于另一个范围。
  • If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal.如果两个范围具有相同的元素并且长度相同,则这两个范围在字典序上是相等的。
  • An empty range is lexicographically less than any non-empty range.空范围按字典顺序小于任何非空范围。
  • Two empty ranges are lexicographically equal.两个空范围在字典上是相等的。

Simplified, it basically means an intuitive element-by-element comparison from left-to-right ( ref ).简而言之,它基本上意味着从左到右 ( ref ) 的直观逐个元素比较。

As for your second question, the contents of a set<int> are in increasing numerical order, irrespective of what order you inserted the elements, so no.至于你的第二个问题,无论你插入元素的顺序如何, set<int>的内容都是按数字递增的顺序排列的,所以不是。

I have read on cppreference that std::set implements the Compare operation via std::lexicographical_compare of two std::set - what does this actually mean?我在 cppreference 上读到std::set通过两个std::set std::lexicographical_compare实现比较操作 - 这实际上意味着什么?

It means that the comparison operator of std::set calls std::lexicographical_compare with the range of the elements of the operands as arguments, and returns the result.这意味着std::set的比较运算符以操作数的元素范围为参数调用std::lexicographical_compare ,并返回结果。 std::lexicographical_compare compares the lexicographical order of the operand ranges. std::lexicographical_compare比较操作数范围的字典顺序。 This is the same order as is used to order words in a dictionary.这与用于对字典中的单词进行排序的顺序相同。

Also, is it possible for two std::set with the same std::uint32_t s inserted, but in different order ...另外,两个std::set是否有可能插入相同的std::uint32_t s,但顺序不同......

std::set is ordered. std::set是有序的。 All sets with same comparison function have the same order.所有具有相同比较函数的集合具有相同的顺序。 Elements that compare less are before elements that compare as higher.比较少的元素在比较高的元素之前。 Thus, if both elements and the comparison function are the same, then the sets will compare equal in lexicographical comparison.因此,如果两个元素和比较函数相同,则集合在字典比较中比较相等。

If the comparison function is stateless, then all instances of it produce the same order.如果比较函数是无状态的,那么它的所有实例都会产生相同的顺序。 In theory, a stateful comparison function could produce a different order for different set.理论上,有状态的比较函数可以为不同的集合产生不同的顺序。 I would recommend to avoid such counter-intuitive comparison functions.我建议避免这种违反直觉的比较函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM