-4

I am declaring a hash map like map<char *, int> min my C++ program. But it was not working, so I followed the instructions from Using char* as a key in std::map and declared my map like map<char *, int, cmp_str> m. My program kind of looks like this

struct cmp_str
{   
    bool operator()(char const *a, char const *b)
    {   
        return std::strcmp(a, b) < 0;
    }
};

int main(int argc, char *argv[])
{
   map<char *, int, cmp_str> m
   //Reading strings from a file 
   while( not end of file ) 
  {
     // char *str contains the line 
     if(m.find(str) != m.end()) {m[str]++; }
     else {m[str] = 1;}

  }
}

When I execute the program, if finds all strings but first even if they are not inserted. When I tried using map<string, int> m;and converted char *str to std::string it works fine. But the input file is so large, it takes lots of time when I use string. I am not sure why it finds all strings when I use char *. Any help would be appreciated.

Community
  • 1
  • 1
CPP_NEW
  • 197
  • 2
  • 9

1 Answers1

3

When you use map<char *, int, cmp_str> m you cannot modify that buffer after you inserted it to std::map as map does not copy data but pointer itself. When you use std::map<std::string,int> std::string does make a copy and that's why it works and it is slower. So you either need to manually create many buffers and store strings in them (and it will make your program slower) or use std::string which is proper and better way.

Slava
  • 43,454
  • 1
  • 47
  • 90
  • That is true. My strings have fixed length 50. Is there any faster way to do this? – CPP_NEW May 13 '16 at 14:35
  • @CPP_NEW maybe yes, but you need to explain what you are trying to achieve. Note if you decide to do that open new question. – Slava May 13 '16 at 14:58
  • I have a big file (~5GB) and I would like to store the count of each line in a hash table. Currently it takes around 10 minutes when I use `std::string`. I want it faster – CPP_NEW May 13 '16 at 15:57
  • @CPP_NEW it depends on data in your file. If there are many repetitions you may want to use radix tree - https://en.wikipedia.org/wiki/Radix_tree – Slava May 13 '16 at 16:22