I've got an ordered container (usually std::vector) with objects, say
BananaApplePeach
and above custom order. Now the objects go through some processing, might have been removed from the container and reinserted, resulting in a new ordered container.
I have stored the order (Banana -> 1, Apple -> 2, Peach -> 3) separately and would like to re-establish that particular custom order in the new ordered container.
The objects are already present at this time (i.e. I cannot simply insert in the desired order).
So rather than swapping excessively with some naive algorithm on my own I was wondering if there's some algorithm in the standard library I could employ in a cunning way..?
std::sort(fruits.begin(), fruits.end(), [](const std::string& a, const std::string& b) { return rank[a] < rank[b]; });whererankisstd::map<std::string, int>holding your string-to-rank mapping?