Can come one explain this code to me about parsing a string?
I found this code that takes a a long string of words separated by spaces. it separates the string in to individual words and stores them in to a vector. then it calls the individual words and does something and then it returns with only the numbers in the words.
I am wondering exactly it separates the numbers from the words.
so in simpler terms it takes this string ""12as 32312dsda sda23asd" and separates it to 3 strings: "12as" "32312dsda" "sda23asd" and puts it in a vector. after that i don't understand what is going after that, on can someone explain it?
Code:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
int i;
int testNumber;
char testChar;
string input;
stringstream ss;
vector<string> myStrings;
vector<int> myIntegers;
cout << "Enter the string : "; getline(cin, input);
ss << input;
while(ss >> input) myStrings.push_back(input);
for(i = 0; i < myStrings.size(); i++)
{
ss.str("");
ss.clear();
ss << myStrings[i];
bool bQuit = false;
while(bQuit == false)
{
if(ss >> testNumber)
{
myIntegers.push_back(testNumber);
bQuit = true;
}
else
{
ss.clear();
if(!(ss >> testChar)) bQuit = true;
}
}
}
cout << "The numbers in the strings : " << endl;
for(i = 0; i < myIntegers.size(); i++)
{
cout << myIntegers[i] << endl;
}
cin.get();
return 0;
}