Map Compare

I am not sure how to get function fun to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

#include <string>

#include <map>

#include <algorithm>


bool fun(std::map<std::string, int> v1, std::map<<std::string, int> v2)
{
	return v1.second > v2.second;
}


int main()

{

	std::map<std::string,int> v;

	v.insert({"Robert", 1050});

	sort(v.begin(), v.end(),fun);

	std::map<std::string,int>::iterator vi;

	for(vi = v.begin(); vi != v.end(); vi++)

	{

		std::cout << vi->first << " " << vi->second << std::endl;

	}



	return 0;

}


A few things.

#1: You have a typo on line 10: std::map<<

#2: Your comparator "fun" should be comparing std::pairs, not std::maps. Not that it matters, because...

#3: You can't apply std::sort to std::map iterators. The reason why is simple: std::maps are already sorted by their keys, and they don't allow you to mix up their internal ordering because that would break them in some pretty fundamental ways.

-Albatross
Topic archived. No new replies allowed.