stl/map and string question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 80
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

stl/map and string question

 
0
  #1
Nov 30th, 2008
I have string and a map<string,int>

i need to iterate through the map and find string[i] 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"

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

i also tried
  1. for(itStr its = str.begin() ; its < str.end(); its++ ){
  2. itf = myMap.find(*its);
  3. myOutfile << (*itf).second;
  4. }

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!

thanks

drjay
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: stl/map and string question

 
0
  #2
Nov 30th, 2008
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...
Last edited by ArkM; Nov 30th, 2008 at 2:35 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC