Re: sorting map value based
On Nov 2, 7:48 am, Philipp Kraus <philipp.kr...@flashpixx.de> wrote:
I need always both key => value and value => key,
I don't know what the means.
For the moment, let's takes the (key, value) pairs and consider it to
just be one object, albeit with different possible sort rules, such as
"Sort only by the key" and "Sort only by the second element of the
pair (the value)". If you need two maintain two different sort orders
at the same time, then a single std::set and std:map will not let you
do this. Each std::set and std::map object have an specific individual
unchanging sort order over its lifetime. If you need the objects (the
pairs) sorted in two different sort orders, then you would need two
different std::set and/or std::map objects, or to use a different data
structure.
If you wanted to use two different std::set or std::map objects, then
you would not need to copy the data. One could simply be a set of
pointers, and you override the sort rule to sort based on pointed-to
values, not the pointer values, ex:
#include <set>
struct int_ptr_comp{ bool operator() (int* x, int* y) { return *x <
*y; } };
std::set<int*, int_ptr_comp> some_set;
It would be up to you to ensure that the two different std::set or
std::map objects remain in sync. This could be done with a simple
class and abstraction, data hiding, and encapsulation.
Alternatively use some Boost container which does this all for you
already (or perhaps uses a more intelligent data structure), as
suggested else-thread.