STL map

Hi there,

I am using the STL map and I print the map using:

map <string, float> ngram_token_index ;
map <string, float>::iterator map_iter ;

//read the map
...

// print the map
for ( map_iter = ngram_token_index.begin() ; map_iter != ngram_token_index.end() ; map_iter++ )
cout << map_iter->first << " => " << map_iter->second << endl;

While this works, I want to copy the value in map_iter->first to a string variable.

The code
char str[1024] ;
strcpy(str, map_iter->first) ;
gives the following error:
error: cannot convert 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'const char*' for argument '2' to 'char* strcpy(char*, const char*)'

I am new to STL. Any help would be greatly appreciated.

Thanks,
J.

That is because what the map actually returned is an STL "string" object rather than a char array.

Try to call c_str() on the "basic_string" you received from the map to get the underlying char pointer before you do the strcpy.

basic_string<charT, traits, Alloc>

Or, you can use STL "string" exclusively instead of using char*. C++ programmers usually prefer to use string objects instead of mixing string and char* anyway.

Hi cbkihong

Thanks a lot. c_str worked for me.

J.