| | |
stl/map and string question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 80
Reputation:
Solved Threads: 1
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"
i also tried
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
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"
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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!
thanks
drjay
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...
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.
![]() |
Similar Threads
- Composed STL types (C++)
- STL funciton return vector of map iterators (C++)
- (was) STL <map> question (C++)
- C++ Question...with uncompleted answer..anyone can solve it? (C++)
- STL <map> question? (C++)
- Is there a 'parent' stl container class? (C++)
- Help with < (less than) operator on classes (C++)
Other Threads in the C++ Forum
- Previous Thread: Does it need a copy constructor and an operator= for a class which have array member
- Next Thread: CString to Integer / Double
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






