stl/map and string question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drjay1627
    New Member
    • Nov 2008
    • 19

    stl/map and string question

    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"

    Code:
    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
    Code:
     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_str ing<_CharT, _Traits, _Alloc>::basic_ string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_trait s<char>, _Alloc = std::allocator< char>]'

    can someone please help me fix this!

    thanks

    drjay
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    If Str is a single string I can understand the error message because you're iterating over single characters. Single chars can not be converted to pointers to chars.

    kind regards,

    Jos

    Comment

    • drjay1627
      New Member
      • Nov 2008
      • 19

      #3
      what is the way around it?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        What you are assuming is that your string is treated as an array of words, and that it has an iterator which accesses each word. This isn't what C++ assumes or performs, though. You can work around it by manually parsing your string to get an array/list of words (hint: each word is separated by a space). Put each word into a list or vector, and use that list/vector's iterator, and the code you have will work fine.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Your loop in incorrectly coded.

          You are looking for strings os the loop does not test i < str.length(). Instead test for the number of strings, that is, the number of elements in the string array.

          Code:
          for(unsigned int i = 0 ; i <4 ; i++){ 
          etc...
          I made this chamge and your code works fine.

          Comment

          • drjay1627
            New Member
            • Nov 2008
            • 19

            #6
            what if the you dont know the length of the string?

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              It's a string object. It knows how long it is.

              You can't use a C++ string object using the C-string techniques of C.

              Comment

              Working...