C
cppaddict
Hi,
I'm confused about what the comparison operator in a map template is:
In particular, I want to know why something like the following doesn't
work:
bool pointCompare(POINT p1, POINT p2) {
if (p1.y > p2.y || (p1.y == p2.y && p1.x > p2.x) )
return true;
return false;
}
int main() {
std::map<POINT,bool,pointCompare> myMap;
return 0;
}
That code won't compile, because pointCompare is not a valid
comparison operator. Below is code that does work -- it creates a
class and then defines the function operator on it. I'm unclear as to
why that method works while the one above does not.
Also, in the line:
bool operator() (const string& s1, const string& s2) const {
what is the final "const" doing there?
Thanks for any clarification,
cpp
-----WORKING CODE FOLLOWS-----
#include <iostream>
#include <iomanip>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
/* function object to compare strings
* - allows you to set the comparison criterion at runtime
* - allows you to compare case insensitive
*/
class RuntimeStringCmp {
public:
// constants for the comparison criterion
enum cmp_mode {normal, nocase};
private:
// actual comparison mode
const cmp_mode mode;
// auxiliary function to compare case insensitive
static bool nocase_compare (char c1, char c2)
{
return toupper(c1) < toupper(c2);
}
public:
// constructor: initializes the comparison criterion
RuntimeStringCmp (cmp_mode m=normal) : mode(m) {
}
// the comparison
bool operator() (const string& s1, const string& s2) const {
if (mode == normal) {
return s1<s2;
}
else {
return lexicographical_compare (s1.begin(), s1.end(),
s2.begin(), s2.end(),
nocase_compare);
}
}
};
/* container type:
* - map with
* - string keys
* - string values
* - the special comparison object type
*/
typedef map<string,string,RuntimeStringCmp> StringStringMap;
I'm confused about what the comparison operator in a map template is:
In particular, I want to know why something like the following doesn't
work:
bool pointCompare(POINT p1, POINT p2) {
if (p1.y > p2.y || (p1.y == p2.y && p1.x > p2.x) )
return true;
return false;
}
int main() {
std::map<POINT,bool,pointCompare> myMap;
return 0;
}
That code won't compile, because pointCompare is not a valid
comparison operator. Below is code that does work -- it creates a
class and then defines the function operator on it. I'm unclear as to
why that method works while the one above does not.
Also, in the line:
bool operator() (const string& s1, const string& s2) const {
what is the final "const" doing there?
Thanks for any clarification,
cpp
-----WORKING CODE FOLLOWS-----
#include <iostream>
#include <iomanip>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
/* function object to compare strings
* - allows you to set the comparison criterion at runtime
* - allows you to compare case insensitive
*/
class RuntimeStringCmp {
public:
// constants for the comparison criterion
enum cmp_mode {normal, nocase};
private:
// actual comparison mode
const cmp_mode mode;
// auxiliary function to compare case insensitive
static bool nocase_compare (char c1, char c2)
{
return toupper(c1) < toupper(c2);
}
public:
// constructor: initializes the comparison criterion
RuntimeStringCmp (cmp_mode m=normal) : mode(m) {
}
// the comparison
bool operator() (const string& s1, const string& s2) const {
if (mode == normal) {
return s1<s2;
}
else {
return lexicographical_compare (s1.begin(), s1.end(),
s2.begin(), s2.end(),
nocase_compare);
}
}
};
/* container type:
* - map with
* - string keys
* - string values
* - the special comparison object type
*/
typedef map<string,string,RuntimeStringCmp> StringStringMap;