This is not a homework.
I\'m using a small \"priority queue\" (implemented as array at the moment) for storing last N items with smallest value. This is a b
If you construct an STL priority queue at the maximum size (perhaps from a vector initialized with placeholders), and then check the size before inserting (removing an item if necessary beforehand) you'll never have dynamic allocation during insert operations. The STL implementation is quite efficient.