comparison functor for std::map

Hey...

I like to use a map which key is of type std::tm.
Since there is no <-operator predefined for std::tm, but std::map expects one, I did define one myself.

I just wanted to ask You guys what you think about it: is it sufficient enough, if we consider that the items stored are at least a few seconds different?

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class CCompareTM
{
public:
	CCompareTM() {};
	~CCompareTM() {};

	bool operator()(const std::tm& t1, const std::tm& t2)
	{
		short sComp = CompareYear(t1,t2);
		if(sComp != 0)
		{
			if(sComp == -1)
				return true;
			return false;
		}
		
		//years equal, compare day
		sComp = CompareYearDay(t1,t2);
		if(sComp != 0)
		{
			if(sComp == -1)
				return true;
			return false;
		}

		//days equal
		sComp = CompareHour(t1,t2);
		if(sComp != 0)
		{
			if(sComp == -1)
				return true;
			return false;
		}

		//hours equal
		sComp = CompareMinute(t1,t2);
		if(sComp != 0)
		{
			if(sComp == -1)
				return true;
			return false;
		}

		//minute equal, last comparison
		sComp = CompareSecond(t1,t2);
		if(sComp != 0)
		{
			if(sComp == -1)
				return true;
			return false;
		}
		
		//a bot enters stuff^^
		return true;
	}

private:
	short CompareYear(const std::tm& t1, const std::tm& t2)
	{
		if(t1.tm_year < t2.tm_year)
			return -1;
		if(t1.tm_year == t2.tm_year)
			return 0;

		return -2;
	};

	short CompareYearDay(const std::tm& t1, const std::tm& t2)
	{
		if(t1.tm_yday < t2.tm_yday)
			return -1;
		if(t1.tm_yday == t2.tm_yday)
			return 0;

		return -2;
	};

	short CompareHour(const std::tm& t1, const std::tm& t2)
	{
		if(t1.tm_hour < t2.tm_hour)
			return -1;
		if(t1.tm_hour == t2.tm_hour)
			return 0;

		return -2;
	};

	short CompareMinute(const std::tm& t1, const std::tm& t2)
	{
		if(t1.tm_min < t2.tm_min)
			return -1;
		if(t1.tm_min == t2.tm_min)
			return 0;

		return -2;
	};

	short CompareSecond(const std::tm& t1, const std::tm& t2)
	{
		if(t1.tm_sec < t2.tm_sec)
			return -1;
		if(t1.tm_sec == t2.tm_sec)
			return 0;

		return -2;
	};
	
};
Looks good. Glad to see another one that uses Pascal casing in functions, hehe. Everybody around here likes all lowercase, or camel casing.
I think that there should be only on return from a function. Also, where is your operator < that you defined?
kooth, he/she will be using std::map<std::tm, SomeType, CCompareTM>, so the operator< is actually the operator() in the CCompareTM class.
Topic archived. No new replies allowed.