Skip to content

Instantly share code, notes, and snippets.

@kkabdol
Created January 15, 2019 14:06
Show Gist options
  • Select an option

  • Save kkabdol/a9419119c9d14e8164339af8ca8fb061 to your computer and use it in GitHub Desktop.

Select an option

Save kkabdol/a9419119c9d14e8164339af8ca8fb061 to your computer and use it in GitHub Desktop.
Extract all integers from string in C++
// https://www.geeksforgeeks.org/extract-integers-string-c/
vector<int> extractIntegerWords(string str)
{
vector<int> v;
stringstream ss;
/* Storing the whole string into string stream */
ss << str;
/* Running loop till the end of the stream */
string temp;
int found;
while (!ss.eof()) {
/* extracting word by word from stream */
ss >> temp;
/* Checking the given word is integer or not */
if (stringstream(temp) >> found)
{
v.push_back( fount );
cout << found << " ";
}
/* To save from space at the end of string */
temp = "";
}
return v;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment