I would like to see if a string contains a double as its sole contents. In other words, if it could possibly be the output of the following function:
string
You can use the Boost lexical_cast to check whether a string contains double or not.
#include
....
using boost::lexical_cast;
using boost::bad_lexical_cast;
....
template bool isValid(const string& num) {
bool flag = true;
try {
T tmp = lexical_cast(num);
}
catch (bad_lexical_cast &e) {
flag = false;
}
return flag;
}
int main(){
// ....
if (isValid(str))
cout << "valid double." << endl;
else
cout << "NOT a valid double." << endl;
//....
}