Вы можете использовать функции потоков, чтобы прочитать число и убедиться, что в потоке больше ничего нет.
std::string str = "31337 test";
std::stringstream stream(str);
int val;
char x;
if ((stream >> val) && !(stream >> x)) {
// We read an integer from the stream.
// **and** there is nothing else on the stream
// (except white space)
}
Полезно, так как вы также можете использовать его с любой конкретной базой:
if ((stream >> std::setbase(8) >> val) && !(stream >> x)) {
// We read an integer from the stream.
// **and** there is nothing else on the stream
// (except white space)
}