Hi! I've read the STL tutorials on this page, but got some problems when I tried to use the code for the map, having strings as keys, with my compiler Ms Visual C++ 5.0.
I've tried to corner the problem and here's a list of things that work:
- creating and using a map with keys as int and values as int (to clear up possible misunderstandings: I'm calling the first parameter key type and the second one value type in this text)
- creating and using a map with keys as int and strings as values
- creating and using strings in many ways
Here's what doesn't work:
- using the ==, < and > operators for strings (but the compare member function of the string class works)
- creating a map with keys as strings and values as something else, for example int.
I suspect that the operators not working might be the cause of the creation of a map with strings as keys giving me compilation errors. I've thought of the following possible ways of solving the problem:
- getting a new compiler - will this solve the problem? Which compiler should I get?
- writing a comparing function for strings and pass it as last parameter to the map class when I create it. In the tutorial it's mentioned that a comparing function can be sent as third parameter when creating a map object, but how do I refer to the function? Just typing the name of the function, like this:
Code:
#include <iostream>
#include <map>
using namespace std;
int strComparator(string first, string second) {
return first.compare(second);
}
int main() {
map<string,int,strComparator> myMap;
return 0;
}
the problem is that I get compiling errors when I try this... What am I doing wrong? How am I to refer to the compare function? Function pointers? Here's my attempt at using function pointers, it gave me the exact same error message as the above code did:
Code:
#include <iostream>
#include <map>
using namespace std;
int strComparator(string first, string second) {
return first.compare(second);
}
int main() {
int (*fPtr)(string,string) = strComparator;
map<string,int,fPtr> myMap;
myMap4["hello"] = 5;
return 0;
}