Registration Forgot password?
Login
Password
Remember me
Login through VKontakte
General > Articles > Problems with the map in C++

Problems with the map in C++

When working with a map in C++ and used char* as key having trouble when try finding item in the map by key.

The essence of the problem is that this type of char* is array of characters ending null character, so compare them have special functions.

So to work properly map need use the following entry map

Sample code. Attention! code is an example extracted from the project is not the fact that it is work.

// compare function for map
struct MapCharACharACompare {
  bool operator() (const char* lhs, const char* rhs) const {return _stricmp(lhs, rhs) < 0;}
};

map tmpMap;

char* getValue(char* Code) {
 map::iterator el = tmpMap.find(Code);
 if (el != tmpMap.end()) {
   return el->second;
 } else {
   return "";
 }
}

int main() {
 tmpMap["TestKey"] = "TestCode";
 std::cout << "TestKey value = " << getValue("TestCode") << std::endl;
 _getch();
 return 0;
}
You can add comment on current page, or on forum page there.Post count 5.

Last comment

13.07.2016
This forum needed shkanig up and you've just done that. Great post! http://lndqszr.com [url=http://sdzdwaxddpv.com]sdzdwaxddpv[/url] [link=http://ipqzxvrgpqk.com]ipqzxvrgpqk[/link]
11.07.2016
A perfect reply! Thanks for taking the trulboe.
10.07.2016

Name*E-mail
Code*