1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Copying one vector to another

Discussion in 'C' started by Lief Webster, May 7, 2008.

  1. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    I've been working on a program that is meant to take a full sentence from the user (saved as a vector<char>) and divide it into words. It does this by using an iterator in a for loop that looks something like this:

    Code:
    for ( *iter = sentence.begin(); *iter != ' '; iter++ )
        {
           words.push_back( *iter );
           // Do some form of copying from 
           // the words vector to a string vector
        }
    (Assuming that sentence is the vector<char> and words is another vector<char>. Also assuming that iter is a normal iterator for a vector<char>.)

    I need a way to fill in the line of code commented in the above block. The eventual outcome of the program is supposed to be the computer displaying each separate word in the sentence, so you can see that I need a way to put each word into a vector of its own. Then I can iterate through it and display each word.

    My other problem is, how can I fill a vector<char> up with a string from the user? For example, if the user input:

    Code:
    Hello world.
    ...how could I assign that to a vector<char> so that the vector<char> held the values

    Code:
    H, e, l, l, o, , w, o, r, l, d, .
    to easily (maybe?) be copied over to a vector<string>?

    I hope this makes sense; if not, I'll be happy to try to clarify. Thanks.
     

Share This Page