How do I input a string then output a vector from this function? The compiler doesnt seem to like "while(invector>>word)".

vector<string> example (string invector);//prototype

vector<string> example(string invector)
{
	
	string word;
	stringstream insert(invector);
	vector < string > vectemp;
	 
	while(invector >> word)
		vectemp.push_back(word); 
  
	 for(int i = 0; i < vectemp.size(); i++)
		cout << vectemp[i] << endl;
  
	return(vectemp);
}

Of course, the C++ compiler does not like overloaded shift operator with string left operand. You want insert >> word , right?
A very unfortunate name for input stringstream: insert...

When I return vectemp back to the main function it acts like a string, but it is suppose to act like a vector. Is it being passed wrong?

When I return vectemp back to the main function it acts like a string, but it is suppose to act like a vector. Is it being passed wrong?

Can you explain this phrase: "it acts like a string"? The vector can't act as a string in principle.

Sure, I pass in the string "This is a string" to the function. And when it leaves I want it to hold the words entirely not just the letters. So when I do something like:

for(int i = 0; i < vectemp.size(); i++)
		cout << vectemp[i] << endl;

Outside the function it will print:
This
is
a
string

>when it leaves I want it to hold the words entirely not just the letters
That's what it appears to do (after ArkM's fix, of course). I'd wager that you're not using the returned vector correctly. Can you post how you use example?

This article has been dead for over six months. Start a new discussion instead.