如何删除 C++ 中的最后 n 个元素地图?
在C++
std::map
中是否有一种很好且简单的方法来查找nth element
?特别是,我正在寻找一种算法来从 map
中删除最后的 k
元素。检索到第 n 个元素的迭代器并调用 std::map::erase 是有意义的。要求是复杂性不会受到影响 - 应该可以在 O(N)
中删除元素范围。
不应仅仅为了删除元素而复制数据。例如,一种解决方案是将数据复制到 std::vector
中,然后对 std::vector
执行 std::nth_element
,然后std::map::find
查找迭代器以便找出从哪里擦除。
其他解决方案之一是仅迭代 std::map
维护要删除的元素数量的计数器变量。这将给出O(n)
。是否可以用STL算法
替换for
循环?
Is there a nice and simple way to find nth element
in C++
std::map
? Particularly I'm looking for an algorithm to erase the last k
elements from the map
. That would make sense to retrieve the iterator to the nth element
and call std::map::erase
. The requirement is that complexity doesn't suffer - that should be possible to erase the element range in O(N)
.
Copy of the data shouldn't be made just to erase the elements. For example one solution is to copy the data into std::vector
, then do std::nth_element
on std::vector
, and then std::map::find
to find the iterator in order to find out where to erase from.
One of other solutions is to just iterate over std::map
maintaining a counter variable on the number of elements to remove. That would give O(n)
. Is that possible to replace the for
loop with STL algorithm
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
映射无法通过索引提供比线性更好的元素访问,例如
vector
或deque
。您可以使用 std::advance 来完成此操作,但所需时间与 k 成正比。如果k
很小,它通常会足够快——它基本上涉及以下k
指针。下面是一个示例:或者,如果您使用 Boost,则可以使用
boost::prior
:否则,您将必须维护单独的索引,或使用不同的容器。如果您不插入和删除太多元素,则类似排序
向量
之类的东西可能是合适的。Maps don't provide better-than-linear access to elements by index, like
vector
ordeque
. You can do this usingstd::advance
, but it takes time that is proportional tok
. Ifk
is small, it will often be fast enough - it basically involves followingk
pointers. Here's an example:Alternatively, if you're using Boost, you can use
boost::prior
:Otherwise, you'll have to maintain a separate index, or use a different container. Something like a sorted
vector
might be appropriate, if you don't insert and remove elements too much.编辑:编辑原始帖子后答案不适用。
Edit: Answer not applicable after edit of original post.