Using a vector element as a string

This is a discussion on Using a vector element as a string within the C++ Programming forums, part of the General Programming Boards category; I would like to use a function that takes a char array[] as argument. Would it be possible to pass ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    Using a vector element as a string

    I would like to use a function that takes a char array[] as argument.
    Would it be possible to pass a vector element as a char array[] ?


    Code:
    vector <string> vLines;
    
    CopyMemory((vLines.at(0)).c_str(), &iLength, sizeof(WORD));
    SendMessage(hWin_2, EM_GETLINE, i, (LPARAM) (vLines.at(0)).c_str());
    
    cout << vLines[0] <<endl;
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You can pass a vector<char> as a char array. You can pass a string as a const char array. You can't pass a string as a non-const char array.

    Your CopyMemory call is weird, it doesn't match what I would expect it to be if you're using Windows, so it's hard to say what you're doing. The line in red is correct if you're not changing the string (e.g. if it's the source). If you are changing the string, you might consider something like:
    Code:
    unsigned buffer_length = ???; // set buffer_length dynamically here
    vector<char> buffer(buffer_length); // create buffer of appropriate size
    
    My_Function(&buffer[0], buffer_length); // fill buffer from function
    
    vLines.at(0).assign(buffer, buffer_length); // asign buffer contents to your string
    Depending on what My_Function does there might be better solutions. For example if the buffer can be filled with a shorter, null-terminated string, you don't want to use assign there, you would make sure there was at least one null character at the end and assign with operator=.
    Last edited by Daved; 11-02-2009 at 03:11 PM.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thanks a lot Daved!

    Actually im trying to rewrite this function to c++ and im wondering what to use.

    Dont know if i need vector of vector or just vector?

    Code:
     int iCount;
         WORD iLength, iOffset;
         TCHAR **Lines;
    
         //Get Number of Lines in Edit Field
         iCount = SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
    
         //If Editbox is empty, exit function
         if(!iCount)
    
              return;
    
         Lines = new TCHAR*[iCount];
    
         //Populate 2D array, Lines[LineIndex][LineText]
         for(int i=0; i<iCount; i++)
         {    iOffset = SendMessage(hEdit, EM_LINEINDEX, i, 0);
              iLength = (WORD)SendMessage(hEdit, EM_LINELENGTH, iOffset, 0);
              Lines[i] = new TCHAR[iLength+sizeof(WORD)+1];
              CopyMemory(Lines[i], &iLength, sizeof(WORD));
              SendMessage(hEdit, EM_GETLINE, i, (LPARAM)Lines[i]);
              Lines[i][iLength] = '\0';
         }
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Ladies and gents let me present to you the happiest man on earth: me.

    I solved the problem!

    Ok first of all i realised that i didnt need multidirectional array.

    Then i used Cpjust's help in another thread and i solved the problem with a vector <char>.

    string vs dynamic char array

    Ps
    Cpjust: vec.push_back( '\0' ); // I can't remember if this is required, but it doesn't hurt.

    Apparrently its working without the '\0'. At least i dont have garbage at the end and nothing missing either.
    Last edited by Ducky; 11-03-2009 at 06:25 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slicing problem with inherited classes.
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2009, 02:29 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21