0

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);
}
3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by Narue
Featured Replies
  • 1
    ArkM 1,090   8 Years Ago

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

1

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...

0

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?

0

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.

0

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

0

>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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.