C++ 如何测试字符串是否包含C++;

C++ 如何测试字符串是否包含C++;,c++,string,C++,String,我想知道字符串是否有数字,或者是否没有数字。是否有一个功能可以轻松做到这一点?可能有以下功能: if (std::string::npos != s.find_first_of("0123456789")) { std::cout << "digit(s)found!" << std::endl; } if(std::string::npos!=s.find_first_of(“0123456789”)){ std::cout给定std::String s if(

我想知道字符串是否有数字,或者是否没有数字。是否有一个功能可以轻松做到这一点?

可能有以下功能:

if (std::string::npos != s.find_first_of("0123456789")) {
  std::cout << "digit(s)found!" << std::endl;
}
if(std::string::npos!=s.find_first_of(“0123456789”)){
std::cout给定std::String s

if( s.find_first_of("0123456789")!=std::string::npos )
//digits
boost::regex-re(“[0-9]”);
const std::string src=“test 123 test”;
匹配结果是什么;
布尔搜索结果=
boost::regex_搜索(src.begin()、src.end()、what、re、boost::match_default);

首先查找
可能是你最好的选择,但我一直在玩iostream facets,所以这里有一个替代方案:

if ( use_facet< ctype<char> >( locale() ).scan_is( ctype<char>::digit,
      str.data(), str.data() + str.size() ) != str.data + str.size() )
if(使用方面(locale())。扫描是(ctype::digit),
str.data(),str.data()+str.size())!=str.data+str.size()
string
更改为
wstring
并将
char
更改为
wchar
,从理论上讲,您可能有机会处理某些亚洲脚本中使用的奇怪的固定宽度数字。

\35;
#include <cctype>
#include <algorithm>
#include <string>

if (std::find_if(s.begin(), s.end(), (int(*)(int))std::isdigit) != s.end())
{
  // contains digit
}
#包括 #包括 if(std::find_if(s.begin(),s.end(),(int(*)(int))std::isdigit)!=s.end()) { //包含数字 }
没有标准可供使用,但制作标准并不困难:

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();
}
编辑:

或者更好的版本(因为它可以与任何容器以及常量和非常量容器一起使用):


奇怪的是,在VC++中不需要强制转换,而在GNU中,如果您删除
isdigit
之前的
std:
部分,也不需要强制转换。有人知道为什么会这样吗?@Manuel:
包括Unix/C
,并且至少在Mac OS X上使用
导入其内容。§17.4.1.2/4似乎是这么说的这种方法是非法的,
不应该定义
::isdigit
。这可能是故意的不符合吗?@Potatoswatter-VSC++2008做了同样的事情,不仅对
ctype
而且对整个C std库都是如此。无论如何,这并不能回答为什么前面有
std:
的版本需要强制转换。这是因为来自
isdigit
是一个类型为
int(*)(int)的函数
已经存在。
std::isdigit
是一个模板,因此如果没有强制转换,它是不明确的。它在
中,但我希望您从
中获取它。所以一旦
完成了它的工作,我们不确定它是否符合要求,但肯定是常见的,
::isdigit
无疑是一个特定的函数,而
>std::isdigit
被重载为
中的函数加上
中的模板。模板的任何实例化都没有返回类型
int
,但如果没有强制转换,就不能排除它们。@Steve-感谢您的精彩解释,我已经忘记了
区域设置
kewl,这是一个向上的投票。不幸的是,我尝试了后来(在Mac OS X中,区域设置有些平淡),全宽数字变成了非数字。这需要O(10*n)步。如果
查找任何
则使用
find\u更快,只需要O(n)步。
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();
}
std::string str("abcde123xyz");
printf("Has digits: %s\n", has_digits(str) ? "yes" : "no");
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;
}
const std::string str("abcde123xyz");
printf("Has digits: %s\n", has_digits(str.begin(), str.end()) ? "yes" : "no");