I was just testing out the substr() method to parse a string's words into a string vector. I was just re-creating what I saw in the book I'm reading so I would understand it, but why I'm not getting the desired effect alludes me. I also tried it with a pre-defined string but still no go.
Here's the outputCode:#include <iostream> #include <string> #include <vector> using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { string aStr; string temp = ""; vector<string> parse; string::size_type i, j; cout << "Type a sentence." << endl; getline(cin, aStr); i = 0; while(i != aStr.size()) { while(isspace(aStr[i]) && i != aStr.size()) i++; j = i; while(!isspace(aStr[j]) && j!= aStr.size()) j++; temp = aStr.substr(i, j); if (i != j) { parse.push_back(temp); i = j; } } cout << "You typed " << parse.size() << " words:" << endl; for (vector<string>::size_type i = 0; i < parse.size(); i++) cout << parse[i] << endl; system("Pause"); return 0; }
Code:Type a sentence. The Quick Brown Fox Jumped Over The Fence! You typed 8 words: The Quick Bro Brown Fox Jumpe Fox Jumped Over The Jumped Over The Fence! Over The Fence! The Fence! Fence! Press any key to continue . . .



LinkBack URL
About LinkBacks


