1

Is this possible? Does it even make sense? For instance, if I have a comparison function for a map where I need (pos 1) as key (in order):

ac ab aa bc ba cb

but then I might want to get order:

aa ab ac ba bc cb

I know I can just get lower/upper and compare from there. Also, how about if I have a comparison function for the latter. Could the prior be done as well via a different comparison function and the same map?

  • Unfortunately, I don't have the option of using boost, although I will check that out. –  Oct 05 '12 at 23:36

1 Answers1

1

An ::std::map is a complex data structure that holds the elements in a particular order. Re-ordering this data structure would imply completely destroying it and re-creating it from scratch. So no, this can't really be done in exactly the way you specify.

But it can sort of be done. It requires multiple maps, and doing it space efficiently for non-trivial data requires you store pointers to your data instead of the data itself. How you handle keys in this case depends on exactly what a key is. In some cases you may want to store pointers to keys as well, or possibly just use the data pointer again and have the comparison function extract the key from the data.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194