如何检查如果给定c ++字符串或char *只包含数字? [英] how to check if given c++ string or char* contains only digits?
本文介绍了如何检查如果给定c ++字符串或char *只包含数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
或从其他方式找到第一个非数字字符。
Or from the other way around find first non digit character.
同样的函数适用于字符串和char *?
Do the same functions apply for string and for char* ?
推荐答案
当然,有很多方法来测试字符串只有数字字符。两种可能的方法是:
Of course, there are many ways to test a string for only numeric characters. Two possible methods are:
bool is_digits(const std::string &str)
{
return str.find_first_not_of("0123456789") == std::string::npos;
}
或
bool is_digits(const std::string &str)
{
return std::all_of(str.begin(), str.end(), ::isdigit); // C++11
}
这篇关于如何检查如果给定c ++字符串或char *只包含数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文