I have string and a map<string,int>

i need to iterate through the map and find string and replace the int with the string.

eg: if map contains

hello 6
world 4
its 3
me 60

if the string is "its me hello world world hello"

output should be "3 60 6 4 4 6"

for(int i = 0 ; i < str.length() ; i++){
         it = mymap.find(str[i]); //it is iterator, mymap is map<string,int> 
          myOutfile << (*it).second; //myOutfile is out stream

i also tried

for(itStr its = str.begin() ; its < str.end(); its++ ){
            itf = myMap.find(*its);
            myOutfile << (*itf).second;

i get the same error

main.cpp:294: error: invalid conversion from `char' to `const char*'
main.cpp:294: error: initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

can someone please help me fix this!



8 Years
Discussion Span
Last Post by ArkM

Regrettably both code snippets are absolutely senseless.
May be better post us your map building code? Is its quality the same?
You need to iterate a source string word by word, not a map. You must get WORDS from the source string, not chars. Use istringstream to extract words from the source string...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.