Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good way of *temporarily* sorting a vector?

I've got a std::vector which I need to sort by selected algorithms for certain operations, but to maintain its original state (e.g. items ordered by when they were entered) the rest of the time.

Obviously I can use std::copy to create a temporary vector and sort that, but I'm wondering if there's a better way, possibly by timestamping the items entered.

Cheers

like image 240
deworde Avatar asked Feb 22 '10 17:02

deworde


1 Answers

You could create a std::vector that holds all the indexes of the first vector. You could then sort the index-vector as you like. This should be fast and most importantly, doesn't mean you have to copy the first vector (which is probably more costly!).

like image 89
monoceres Avatar answered Sep 27 '22 22:09

monoceres