1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Copying one vector to another

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

  1. Lief Webster

    Lief Webster New Member

    Oct 16, 2007
    Likes Received:
    Trophy Points:
    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:

    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:

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

    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