?
Solved

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

Posted on 2009-12-16
7
Medium Priority
?
423 Views
Last Modified: 2013-11-13
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!"
0
Comment
Question by:pascal_lalonde
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 6

Expert Comment

by:palinitr
ID: 26067463
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
 
LVL 6

Expert Comment

by:palinitr
ID: 26067469
Sorry, ignore above post, this was by accident - wrong question
0
 
LVL 12

Accepted Solution

by:
Let_Me_Be earned 400 total points
ID: 26067508
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 32

Expert Comment

by:phoffric
ID: 26067664
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
 
LVL 12

Expert Comment

by:Let_Me_Be
ID: 26067681
> 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
 

Author Closing Comment

by:pascal_lalonde
ID: 31667066
Thank you!
0
 

Author Comment

by:pascal_lalonde
ID: 26071641
Thank you!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Make the most of your online learning experience.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question