Thread: Problems with strings as key in STL maps

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    Problems with strings as key in STL maps

    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;
    }
    Last edited by all_names_taken; 01-16-2006 at 12:08 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    map expects a functor to compare the keys.
    here's an example
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    class strless {
       public:
          bool operator() (const string & first, const string & second ) const  { 
             return first < second; 
          }
    };
    
    int main() {
       strless a;
       map<string,int,strless> myMap;
       myMap["a"] = 1;
       myMap["b"] = 2;
       myMap["c"] = 3;
       for ( map<string,int,strless>::const_iterator i = myMap.begin(); i != myMap.end(); ++i ) {
          cout << (*i).first << " " << (*i).second << endl;
       }
       return 0;
    }
    You need to do some reading about the ordering requirements of the various STL-containers.

    About the compiler. I use MSVC 6.0 occasionally and that can hardly handle the STL. If you really try to use 5.0 you should get a new one.


    Kurt
    Last edited by ZuK; 01-16-2006 at 12:55 PM.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    Thanks but unfortunately it doesn't work because it uses the "<" operator that isn't defined for the string class in Ms Visual C++ 5.0 from what it seems to me...

    However I eventually got this code below to work, but it feels like a very clumsy solution to me.

    Edit: ok, if you have probs even in 6.0 I should probably look for another compiler than my old 5.0... Now I at least know it's not me doing anything wrong, which is a revelation

    Code:
    #include <iostream>
    #include <map>
    
    using namespace std;
    
    class String : public string {
    public:
    	String() : string("") {};
    	String(const char *str) : string(str) {};
    
    	bool operator< (string s) {
    		return (this->compare(s) == -1);
    	};
    	bool operator> (string s) {
    		return (this->compare(s) == 1);
    	};
    	bool operator== (string s) {
    		return !(this->compare(s));
    	};
    };
    
    bool operator< (string s, string t) {
    	return s.compare(t) == -1;
    }
    bool operator> (string s, string t) {
    	return s.compare(t) == 1;
    }
    bool operator== (string s, string t) {
    	return !s.compare(t);
    }
    
    int main() {
    	map<String,int> myMap;
    	myMap["something"] = 10;
    
    	return 0;
    }
    The question I have now is how STL handles the insertion of elements. Does it use references to the data or does it copy whatever I put in the map?
    Last edited by all_names_taken; 01-16-2006 at 01:22 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The first thing to try is to include the <string> header. I have seen VC++ allow string declarations when you #include <iostream>, but not allow all the extra operators. Besides, it is not correct to use string unless you #include <string>. Then, try this code:
    Code:
    #include <iostream>
    #include <string>
    #include <map>
     
    using namespace std;
     
    int main() {
    	map<string,int> myMap;
    	myMap4["hello"] = 5;
    	return 0;
    }
    If that doesn't work, then there is probably a problem with your compiler. Version 5.0 is very old, even 6.0 isn't very standards compliant. I'd suggest going to version 7.1 or later (.NET 2003 or 2005).

    All that extra code to work around your problem shouldn't be necessary, and if it isn't solved by #including <string> (or even if it is), switching compilers is probably the better idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stl string problems with the + operator
    By mangoMan in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2004, 04:50 PM
  2. Problems with strings and for loops
    By petedee in forum C Programming
    Replies: 52
    Last Post: 04-02-2004, 03:53 AM
  3. More problems with C style strings
    By mousey in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2003, 01:52 PM
  4. stl fstream error (yes... file problems again)
    By ygfperson in forum C++ Programming
    Replies: 7
    Last Post: 07-13-2002, 10:47 PM