Click to See Complete Forum and Search --> : Vector of Strings: help!


assaf2b
April 12th, 2008, 11:52 AM
I need to write a C++ program taking a vector of strings and separating out the words in each, storing them into a vector, but I don't know how. Could anyone help me please?


Template Code:

vector <string> seperateWords (vector<string> lines) {
vector<string > words;

//Separating stuff goes here

return words;
}


Thanks in advance!

Plasmator
April 12th, 2008, 12:11 PM
Use a stringstream to do the "dirty" work for you.
Here's a minimal (and untested) example:
std::vector<std::string> Foo(const std::string& data)
{
std::vector<std::string> result;

std::istringstream converter(data);
std::string buffer;

while(converter >> buffer)
{
result.push_back(buffer);
}

return result;
}

assaf2b
April 12th, 2008, 12:13 PM
Thanks. Could you please explain what exactly have you done there?

Plasmator
April 12th, 2008, 12:16 PM
Thanks. Could you please explain what exactly have you done there?What is it that you don't understand?

assaf2b
April 12th, 2008, 12:20 PM
Everything, really. I was given this assignment in order to be accepted into a class, but I know little to nothing (as of now) regarding C++.

Plasmator
April 12th, 2008, 12:22 PM
You can't run before you learn how to walk.
I suggest you pick up a (good) book and start reading ASAP if you want to get into that class.

assaf2b
April 13th, 2008, 12:57 PM
Could anyone please just write, as a comment, next to each line what it's supposed to mean... as in, why is it there?

I'd highly appreciate it.

7stud
April 13th, 2008, 01:36 PM
Why do you think you should be accepted into the class if you can't do the assignment?

Paul McKenzie
April 13th, 2008, 01:46 PM
Could anyone please just write, as a comment, next to each line what it's supposed to mean... as in, why is it there?With all due respect, that is ridiculous. No one learns programming that way, no matter what programming language is being taught.

Regards,

Paul McKenzie

Zaccheus
April 14th, 2008, 04:05 AM
Thanks. Could you please explain what exactly have you done there?
Look at the documentation for istringstream and it should be fairly obvious what he has done there.
:)