There is nothing standard for this purpose, but it is not difficult to do:
template <typename CharT> bool has_digits(std::basic_string<CharT> &input) { typedef typename std::basic_string<CharT>::iterator IteratorType; IteratorType it = std::find_if(input.begin(), input.end(), std::tr1::bind(std::isdigit<CharT>, std::tr1::placeholders::_1, std::locale())); return it != input.end(); }
And you can use it like this:
std::string str("abcde123xyz"); printf("Has digits: %s\n", has_digits(str) ? "yes" : "no");
Edit:
Or even a better version (since it can work with any container and with const and non-const containers):
template <typename InputIterator> bool has_digits(InputIterator first, InputIterator last) { typedef typename InputIterator::value_type CharT; InputIterator it = std::find_if(first, last, std::tr1::bind(std::isdigit<CharT>, std::tr1::placeholders::_1, std::locale())); return it != last; }
And this one you can use, for example:
const std::string str("abcde123xyz"); printf("Has digits: %s\n", has_digits(str.begin(), str.end()) ? "yes" : "no");
Dmitry
source share