Speed up code based on map with vectors as values
You can use std::iota for the sequential order vector. If it doesnt increase speed it is prettier:

std::vector&lt;short&gt; order(len);
std::iota(order.begin(), order.end(), 0);


The same obviously goes for indexes
Given that you emplace in sequential order here selected.emplace(lnum, vector&lt;Graphic*&gt;()) you might want to switch to emplace_back
If you do not care about memory you can reserve the memory for your graphics vector with a guess during initialization and then call shrink_to_fit afterwards. That way you avoid possibly costly reallocations. However depending on the size of your guess this might significantly increase the memory burden.