根据插入时间从std :: map中删除元素 [英] Remove element from std::map based on the time of insertion

查看:169
本文介绍了根据插入时间从std :: map中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据插入的时间从std :: map中删除元素(或者比其他东西更有效)。

I need to erase elements from an std::map based on the time of insertion (or something else more efficient than that).

地图可能会保持成千上万的元素,如果我存储时间并迭代地图来检查每个元素的时间,那么可能最终会耗费时间。

The map will probably hold thousands of elements and if I store the time and iterate the map to check each elements time, it will probably end up being quite time consuming.

有没有人有什么好主意

推荐答案

std: :map<> type没有插入元素的概念。它仅用于保存键/值对映射。它也没有插入顺序的概念,因此它甚至不能提供相对类型的插入。

The std::map<> type has no notion of when an element was inserted. It only serves to hold a key / value pair mapping. It also has no notion of insert order so it can't even provide a relative type of insert.

为了做你想要的,你需要添加一个关联元素和插入的时间。如果您想要的是相对顺序,那么您可以使用与地图配对的 std :: queue 。每次插入地图时,您也插入到 std :: queue 中。队列前面的元素比后面的元素要老,可以使用相对年龄

To do what you want you'll need to add an association between the elements and the time they were inserted. If all you want is relative order then you could use a std::queue paired with the map. Every time you insert into the map you insert into the std::queue as well. Elements at front of the queue are older than the back and you can use that for relative age

这篇关于根据插入时间从std :: map中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆