Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ Data Structure that remains sorted by value & operates FIFO

I am looking for a data structure that performs fast sorted insertion and operates based on FIFO.

What I am trying to achieve is a data structure of a fixed size to hold a series of values. At each new step of an iteration, I want to efficiently be able to find the min or max value (so I want the data structure to be sorted at all times) and upon the request to insert a new element, the oldest element be automatically (or at least able to be efficiently) popped / discarded.

So I guess I am looking for some sort of of FIFO priority queue.

Any help much appreciated.

like image 798
oracle3001 Avatar asked Nov 29 '25 16:11

oracle3001


1 Answers

Why not have both an std::set or multiset, and a regular FIFO queue like std::queue of iterators into that set? At every insertion, check if the queue became bigger than your maximum size, then remove the front element from the queue and the set.

like image 194
Tavian Barnes Avatar answered Dec 02 '25 05:12

Tavian Barnes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!