Remove element from std::map based on the time of insertion

后端 未结 3 1450
栀梦
栀梦 2021-02-20 00:30

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 a

3条回答
  •  無奈伤痛
    2021-02-20 00:35

    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.

    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

提交回复
热议问题