Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Copying one vector to another (http://www.go4expert.com/forums/copying-vector-t10455/)

Lief Webster 8May2008 04:02

Copying one vector to another
 
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.


All times are GMT +5.5. The time now is 18:35.