|
-
April 12th, 2008, 11:52 AM
#1
Vector of Strings: help!
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:
Code:
vector <string> seperateWords (vector<string> lines) {
vector<string > words;
//Separating stuff goes here
return words;
}
Thanks in advance!
Last edited by assaf2b; April 12th, 2008 at 11:53 AM.
Reason: Addition of Code tags.
-
April 12th, 2008, 12:11 PM
#2
Re: Vector of Strings: help!
Use a stringstream to do the "dirty" work for you.
Here's a minimal (and untested) example:
Code:
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;
}
-
April 12th, 2008, 12:13 PM
#3
Re: Vector of Strings: help!
Thanks. Could you please explain what exactly have you done there?
-
April 12th, 2008, 12:16 PM
#4
Re: Vector of Strings: help!
 Originally Posted by assaf2b
Thanks. Could you please explain what exactly have you done there?
What is it that you don't understand?
-
April 12th, 2008, 12:20 PM
#5
Re: Vector of Strings: help!
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++.
-
April 12th, 2008, 12:22 PM
#6
Re: Vector of Strings: help!
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.
-
April 13th, 2008, 12:57 PM
#7
Re: Vector of Strings: help!
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.
-
April 13th, 2008, 01:36 PM
#8
Re: Vector of Strings: help!
Why do you think you should be accepted into the class if you can't do the assignment?
-
April 13th, 2008, 01:46 PM
#9
Re: Vector of Strings: help!
 Originally Posted by assaf2b
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
-
April 14th, 2008, 04:05 AM
#10
Re: Vector of Strings: help!
 Originally Posted by assaf2b
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|