troubleshooting Question

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

Avatar of pascal_lalonde
pascal_lalonde asked on
ProgrammingC++
7 Comments1 Solution438 ViewsLast Modified:
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!"
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros