std::set 中的索引或位置

发布于 2024-08-12 13:28:38 字数 132 浏览 3 评论 0原文

我有一个 std::std::string 集。我需要集合中每个字符串的“索引”或“位置”,这在上下文中是一个有意义的概念吗?

我猜 find() 会返回一个迭代器到字符串,所以我的问题可能更好地表述为:“如何将迭代器转换为数字?”。

I have a std::set of std::string. I need the "index" or "position" of each string in the set, is this a meaningful concept in the context?

I guess find() will return an iterator to the string, so my question might be better phrased as : "How do I convert an iterator to a number?".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋凉 2024-08-19 13:28:38

std::distance 是您所需要的。我猜你会想要std::distance(set.begin(), find_result)

std::distance is what you need. You will want, I guess std::distance(set.begin(), find_result)

南笙 2024-08-19 13:28:38

我认为这没有意义 - 集合是“自键控”并排序的,因此当集合被修改时,“索引”将失效。

当然,这取决于您打算如何使用索引以及该集合是否本质上是静态的(例如字典)。

I don't think it is meaningful - set's are 'self keyed' and sorted thus the 'index' would be invalidated when the set is modified.

Of course it depends upon how you intend to use the index and if the set is essentially static (say a dictionary).

何必那么矫情 2024-08-19 13:28:38

尽管其他人在这里写了一些内容,但我认为“索引”或“位置”对于集合没有意义。用数学术语来说,集合仅公开其成员,或许还公开其基数。唯一有意义的操作包括测试一个项目是否是该集合的成员,以及组合或减去集合以产生新的集合。

有些人用更宽松的术语将集合视为数据结构,通过“有序”或“无序”的方面,以及它们是否允许重复或强制唯一性。前一个方面区分具有 O(n) 插入防护的数组,其中尝试插入项目时首先会扫描现有成员以查看新项目是否存在,如果不存在,则插入新项目最后是一个哈希表,可能仅在存储桶的链中保留这种顺序。 std::set 使用的红黑树之类的树介于两者之间;其遍历顺序相对于所施加的严格弱顺序是确定的通过比较器谓词,但是,与上面概述的数组不同,它不保留插入顺序。

另一个方面——集合是否允许重复元素——在数学中毫无意义,更准确地描述为“包”。这样的结构承认身份和基于价值的“相同性”之间的差异。

你的问题可能涉及到关心某个职位;目前尚不清楚该位置的含义,但我希望您需要一些与 std::set 分开的数据结构来正确建模。也许从元素集到每个位置的 std::map 映射就可以了。这并不能保证这些职位是独一无二的。

思考如何将其建模为关系(例如在关系数据库中)也可能有助于澄清问题。密钥由什么组成?实体的哪些部分可以独立变化?

Despite what others have written here, I don't think that "index" or "position" has meaning with respect to a set. In mathematical terms, a set exposes only its members and maybe its cardinality. The only meaningful operations involve testing whether an item is a member of the set, and combining or subtracting sets to yield new sets.

Some people talk about sets as data structures in looser terms, by facets of being "ordered" or "unordered", and whether they permit duplicates or enforce uniqueness. The former facet distinguishes an array with an O(n) insertion guard, where an attempt to insert an item first scans the existing members to see if the new item exists and, if not, inserts the new item at the end, and a hash table, that might retain such order only within a bucket's chain. A tree such as the Red-Black Tree used by std::set is somewhere in between; its traversal order is deterministic with respect to the strict weak order imposed by the comparator predicate, but, unlike the array sketched above, it doesn't retain insertion order.

The other facet — whether the set permits duplicate elements — is meaningless in mathematics, and is more accurately described as a bag. Such a structure acknowledges the difference between identity and value-based "sameness."

Your problem may involve caring about some position; it's not clear what that position means, but I expect you're going to need some data structure separate from std::set to model this properly. Perhaps a std::map mapping from your set of elements to each position would do. That would not guarantee that the positions are unique.

It may also help clarify the problem to think how you'd model it as relations, such as in a relational database. What comprises the key? What portions of the entities can vary independently?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文