Hi, I've used getline and pushed back a text row into a vector.
The vector is of string type and I want to sort out the numbers and store it elsewhere.

in my vector: The man is 67 years old and driving an Oldsmobile Rocket 88

I want to sort out 67 and 88.

I've seen examples using char but i really want to sort out numbers from the text larger than one digit. Any ideas?

1. Create istringstream (f.e. istringstream my_stream ) from string stored in vector.
2. Create a while loop (until you reach eof of the stream)
3. Use operator >> and try to save to int variable (f.e. int my_int ). my_stream>>my_int; 4. Check state of the stream. If it returns false, means that it wasn't a number. Reset the state of the stream (clear()) and use operator >> for string variable (f.e. string my_string ). my_stream>>my_string 5. If checking stream state returns true, means that You read integer.
6. Repeat.

try this string parsing algorithm to split your line up into individual strings:


int prev = 0;
int curr = 0;
string temp;
string line = "The man is 67 years old and driving an Oldsmobile Rocket 88";
vector<string> vstring;

     prev = curr;
     curr = line.find(' ');

     if(curr == string::npos)
          temp = line.substr(prev);
          temp = line.substr(prev, curr-1);

  }while(curr != string::npos);

Now you can just test elements of the vstring vector to get what you want:

string word;

cout << "Enter word to find: ";
cin >> word;

for(int i=0, size=vstring.size(); i<=size; i++)
     if(word == vstring[i])

if(i == size)
     cout << "word not found";

     cout << word << " found at element " vstring[i];
It's pi
This article has been dead for over six months. Start a new discussion instead.