Thread: Copying *Specific* Elements From VectorA to VectorB :: C++

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Copying *Specific* Elements From VectorA to VectorB :: C++

    Hi.

    Let say there are two vectors, vectorA and vectorB.

    -----------------------------------------------------
    string dataTemp;
    std::vector<string> vectorA, vectorB;

    for (int = 0; i < 10; ++i)
    {
    dataTemp += i;
    vectorA.push_back(dataTemp);
    }

    /* The loop above should produce vectorA to look something like this:
    0
    1
    12
    123
    1234
    12345
    123456
    1234566
    12345678
    123456789
    */

    Okay, vectorB is empty.

    I want to copy *specific* elements from vectorA to vectorB.

    For example:

    vectorB.push_back(vectorA[4]); // Is that right?
    vecotrB.push_back(vectorA[8]);

    Thanks,
    Kuphryn

  2. #2
    Unregistered
    Guest
    you might need to cast i to a string (maybe the compiler will do it automatically for you but I don't think so) first, and a vector of strings is similar to a 2D char array so you need to specify which string from which vector you want to add to, but then ya, the code you have should work. It will append the char from the string in vector A to the end of the string, although there is an insert function to place the new char somewhere other than the end if you wish.

    vectorB[3].pushback(vectorA[8][7];

    add the eigth char in the ninth string from vectorA to the end of the fourth string in vectorB.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  2. Randomly rearranging the elements in a vector
    By Signifier in forum C++ Programming
    Replies: 11
    Last Post: 08-01-2007, 12:21 PM
  3. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 11:30 AM
  4. Replies: 2
    Last Post: 08-03-2003, 10:01 AM
  5. Array elements values not being assigned
    By Sardien in forum C Programming
    Replies: 4
    Last Post: 02-11-2002, 01:45 PM