Created
January 15, 2019 14:06
-
-
Save kkabdol/a9419119c9d14e8164339af8ca8fb061 to your computer and use it in GitHub Desktop.
Extract all integers from string in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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