How to order map (stl container)?

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!"
You can't sort a map because maps are inherently sorted. Sorting them by the value is even more impossible.
What you're looking for is an std::set, which only uses a value and no key, and is sorted by the value

By the way, I believe your comparison function will cause infinite recursion.
std::map<int,std::string> map; will produce the expected result.
Last edited on
Topic archived. No new replies allowed.