C++ boost - 是否有一个像队列一样工作的容器,可以直接进行键访问?

发布于 2024-10-07 00:16:30 字数 179 浏览 0 评论 0原文

我想知道一个类似队列的容器,但它有按键访问,就像地图一样。 我的目标很简单:我想要一个 FIFO 队列,但是,如果我插入一个元素并且具有给定键的元素已在队列中,我希望它的新元素替换已经存在的元素队列。例如,按插入时间排序的地图就可以。

如果没有这样的容器,你认为可以同时使用队列和映射来实现吗?

I was wonndering about a queue-like container but which has key-access, like a map.
My goal is simple : I want a FIFO queue, but, if I insert an element and an element with a given key is already in the queue, I want it the new element to replaced the one already in the queue. For example, a map ordered by insertion time would work .

If there is no container like that, do you think it can be implemented by using both a queue and a map ?

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

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

发布评论

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

评论(3

泪是无色的血 2024-10-14 00:16:30

Boost multi-index提供了这种容器。

为了自己实现它,我可能会选择一个 map ,其值由链表节点和有效负载组成。列表节点可以是手动滚动的,也可以是 Boost 侵入式< /a>.

请注意,队列适配器的要点是隐藏 Sequence 的大部分接口,但您想弄乱它隐藏的细节。因此,我认为您应该致力于重现 queue 的接口(稍微修改一下 push 的语义),而不是实际使用它。

Boost multi-index provides this kind of container.

To implement it myself, I'd probably go for a map whose values consist of a linked list node plus a payload. The list node could be hand-rolled, or could be Boost intrusive.

Note that the main point of the queue adaptor is to hide most of the interface of Sequence, but you want to mess with the details it hides. So I think you should aim to reproduce the interface of queue (slightly modified with your altered semantics for push) rather than actually use it.

薯片软お妹 2024-10-14 00:16:30

显然,您可以简单地使用类似队列的容器来完成您想要的操作,但是您必须在每次插入时花费 O(n) 时间来确定元素是否已经存在。如果您基于 std::vector 之类的东西实现队列,您可以使用二分搜索并基本上加快插入 O(log n) (即当内存重新分配完成时,仍然需要 O(n) 操作)。

如果这样没问题,就坚持下去。 带有附加容器的变体可能会可以提高性能,但编写起来也很容易出错,如果第一个解决方案足够了,就使用它。


在第二种情况下,您可能希望将元素存储在不同的容器中两次 - 原始队列和诸如映射之类的东西(或者有时哈希映射可能表现更好)。该映射仅用于确定该元素是否已存在于容器中 - 如果是,您将必须在队列中更新它。

基本上,这为我们提供了 O(1) 哈希映射查找的复杂性(在现实世界中,由于冲突,这可能会变得更难看 - 哈希映射并不真正适合确定元素存在)不需要更新的情况的 O(1) 插入时间和需要更新的情况的 O(n) 插入时间。

根据实际更新操作的百分比,实际插入性能可能从 O(1)O(n) 不等,但此方案肯定会优于第一种方案如果更新的数量足够小。

不过,您必须同时将元素插入两个容器中,如果删除该元素,也应该执行相同的操作,并且我会三思而行“我真的需要性能提升吗?”。

Obviously what you want can be done simply with the queue-like container, but you would have to spend O(n) time on every insertion to determine if the element is already present. If you implement your queue based on something like std::vector you could use the binary search and basically speed up your insertion to O(log n) (that would still require O(n) operations when the memory reallocation is done).

If this is fine, just stick to it. The variant with additional container might give you a performance boost, but it's also likely to be error-prone to write and if the first solution is sufficient, just use it.


In the second scenario you might want to store your elements twice in different containers - the original queue and something like a map (or sometimes a hashmap may perform better). The map is used only to determine if the element is already present in the container or not - and if YES, you will have to update it in your queue.

Basically that gives us O(1) complexity for hashmap lookups (in real world this might get uglier because of the collisions - hashmaps aren't really good for determining element existence) and O(1) insertion time for the case when no update is required and O(n) insertion time for the case update is needed.

Based on the percentage of the actual update operations, the actual insertion performance may vary from O(1) to O(n), but this scheme will definitely outperform the first one if the number of updates is small enough.

Still, you have to insert your elements in two containers simultaneosly and the same should be done if the element is deleted and I would think twice "do I really need that performance boost?".

空名 2024-10-14 00:16:30

我看到使用队列和可选的地图来完成此操作的简单方法。

为您的元素定义某种 == 运算符。

然后只需建立一个队列,并在每次要插入元素时搜索它。

您可以通过元素位置到元素的映射来优化这一点,而不是每次都搜索队列。

I see easy way of doing this with a queue and optionally a map.

Define some sort of == operator for your elements.

Then simply have a queue and search for your element every time you want to insert it.

You could optimize this by having a map of element locations to elements instead of searching the queue every time.

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