Here’s the code. I have no idea why it doesn’t recognize that it needs to copy the memory, and I can’t force it to.
string message="The quick brown fox jumped over the lazy dog.";
vector<char*> words;
while(message.length()>0){
char wtf[message.substr(0,message.find(" ")).c_str().length()]=message.substr(0,message.find(" ")).c_str();
words.push_back(wtf);
message=message.substr(message.find(" ")+1);
}
I see that there are similar threads, but none on this. Also, it seems like a shortcomming that C++ can’t deal with this that easily.
How to break text into words (the easy way)
A break down on how it works (for 12 year old’s learning to program)
The operator >> when applied to a stream (and a string) reads a single (white) space separated word.
The istream_iterator is an adapter for streams that make them look like containers.
It reads items from the stream of type ‘T’ using the operator >>
The standard algorithms all work on iterators.
std::copy iterates over the source and places each item in the destination:
The back_inserter is an adapter that uses push_back to add items to the container.
We could have made sure that the destination vector was the correct size. But it is easier to use the back_inserter to make sure the vector is dynamically sized.
Putting it all back together: