Registration Forgot 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;
 return 0;
You can add comment on current page, or on forum page there.

Last comment