This will most probably work with a stringstream in an easy way.
As written in the comments, it is better to use a "regex" for this. Very fortunately C++ has a build-in "regex" library. You may read here about it.
And even better, there is an iterator, with which you can iterate over all patterns in a std::string
: the std::sregex_token_iterator.
This gives you very powerful possibilities, because you can match many different patterns by a regex. Please read about it here.
With that, you can come up with a very simple program like the below:
#include <iostream>
#include <string>
#include <list>
#include <regex>
#include <algorithm>
// Simple Regex for optional sign and digits
const std::regex re{ "[+-]?\\d+" };
int main() {
// The test string
std::string test{ "U<sub>54778</sub><br+123ddd 4 -55 66" };
// Here we will store our integers
std::list<int> integersList{};
// Get all integers
std::transform(std::sregex_token_iterator(test.begin(), test.end(), re), {}, std::back_inserter(integersList), [](const std::string& s) { return std::stoi(s); });
// Show debug output
for (const int i : integersList) std::cout << i << ' ';
}
Please note: If you need to validate the correct format or range for an integer, then the regex will get more complicated.