How to order a map (stl container) at creation by using the value field rather than the key?

Hello everybody!

I use a map and I want to order the elements not by using the key but
by using the value. I know how to do it with the key but not with the value.
I joined my source code. You will see that I first tried successfully to order with the key (macro CORRECT). Can you complete, rearrange my code for the #else?

My code:


#include "windows.h"
#include <string>
#include <map>

using namespace std;

#ifdef CORRECT
bool Compare(int a, int b)
{
  if (b <= a)
      return true;

  return false;
}
#else

class Bottin
{
private:
      map<int, string,  bool (*)(int, int)> donnees;

public:
      bool Compare(int a, int b)
      {
            if (donnees[b] <= [a])
                  return true;

            return false;
      }

      Bottin()
      {
            donnees = map<int, string,  bool (*)(int, int)>(Bottin::Compare);
      }
};
#endif

int main()
{
#ifdef CORRECT
      map<int, string,  bool (*)(int, int)> bottin(Compare);

      bottin[23] = "Boston";
      bottin[26] = "Frisco";
      bottin[21] = "Varadero";
#else

      Bottin bottin;

#endif

      int i = 0;
}


====================================

Thank you!

"Execution is the name of the game!"
pascal_lalondeAsked:
Who is Participating?
 
Let_Me_BeConnect With a Mentor Commented:
You can't. If you want to compare by value, you need to inverse the map (make value the key and key the value).
If you have multiple identical values for different keys, you have to use multimap (after the inversion).

Or you could push the content into a vector of pairs and sort it by the second item in the pair.
Or you could use set of pairs instead of a map.
0
 
palinitrCommented:
Oops, sorry forgot to add that:

access-list dmz_access_in extended permit tcp host 192.168.5.2 any eq http

This will give 192.168.5.2 access to the internet over port 80
0
 
palinitrCommented:
Sorry, ignore above post, this was by accident - wrong question
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
phoffricCommented:
To use qsort, create an array of pointers to the elements in the map. Define a comparison function for qsort that is based on value rather than key.
0
 
Let_Me_BeCommented:
> To use qsort, create an array of pointers to the elements in the map. Define a comparison function for qsort that is based on value rather than key.

Just a note, if you modify the map, such pointers are likely to become invalid.
0
 
pascal_lalondeAuthor Commented:
Thank you!
0
 
pascal_lalondeAuthor Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.