Thread: Vector of Strings help

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    9

    Vector of Strings help

    I have a vector of chars and I'm trying to put the chars into a vector of strings:



    Code:
    int iByteCounter = 0;
    vector<char> vc1TempChar;
    vector<char> vc2TempChar;
    vector<string> vs1String(1000);
    vector<string> vs2String(1000);
    vector<int> viBeginByte;
    vector<stringmatch> vsmStringMatch;
    struct stringmatch
        {
               int b1; // Beginning of string in first file
               int e1; // End of string first file
               int b2; // Beginning of sring 2nd file
               int e2; // end of string 2nd file
               vector<char> vcStringMatch;
        };
    vector<stringmatch> vsmStringMatch;
       .................
                  vs1String.push_back(vc1TempChar[iByteCounter]); // line '1'
                  vs2String.push_back(vc2TempChar[iByteCounter]); // line '2'
                  viBeginByte.push_back(iByteCounter);
                  vsmStringMatch.push_back(smTempStringMatch);
    The problem is that lines '1' and '2' (and other lines like them) produce the following error:

    [error]
    invalid conversion from `char' to `const char*'
    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>]'
    [/error]

    What does the error mean and what did I do wrong?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    vc1TempChar[iByteCounter] is a char. You can't add a char to a vector of strings; you can add a string (or it's C equivalent, which is an (pointer to an) array of chars).

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    invalid conversion from `char' to `const char*'
    Sounds like that could be solved with the '&' operator.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    9
    So would
    Code:
    char* pcTemp;
    
    pcTemp = vc1TempChar[iByteCounter];
    vs1String.push_back(pcTemp);
    work?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No. There are two chances that any particular character is a valid pointer: slim and none, and slim isn't looking very well. Do you want each string to be one character long? If not, maybe you want to push back on the string itself, rather than on the vector of strings.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    9
    No, but the program adds to the string one character at a time.

    Code:
    string sTempString;
    
    
    vs1String.push_back(sTempString);
    vs1String[thisone].push_back(vc1TempChar[iByteCounter]);
    This seems to work
    Last edited by CowsOFFmylawn; 01-11-2009 at 09:33 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So push_back on the string itself, not the vector of strings (you can only add a string to a vector of strings). That is, you need
    Code:
    vs1String[whichever_string_youre_on].push_back(vc1TempChar[iByteCounter]);

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    9
    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM