【问题标题】:Function to check if string contains a number检查字符串是否包含数字的函数
【发布时间】:2012-03-27 09:21:37
【问题描述】:

我正在使用 c++ 开发一个项目(我刚开始学习),但不明白为什么这个功能不起作用。我正在尝试使用变量 first_name 编写“Person”类,并使用函数 set_first_name 来设置名称。 Set_first_name 需要调用一个函数(下面的那个)来检查名称中是否有任何数字。该函数总是返回false,我想知道为什么?另外,这是检查数字的最佳方法,还是有更好的方法?

   bool Person::contains_number(std::string c){ // checks if a string contains a number
        if (c.find('0') == std::string::npos || c.find('1') == std::string::npos || c.find('2') == std::string::npos || c.find('3') == std::string::npos
        || c.find('4') == std::string::npos || c.find('5') == std::string::npos || c.find('6') == std::string::npos || c.find('7') == std::string::npos
        || c.find('8') == std::string::npos || c.find('9') == std::string::npos){// checks if it contains number

        return false;
        }
        return true;
    }

【问题讨论】:

标签: c++ class function numbers


【解决方案1】:

将您所有的|| 更改为&&

更好:

return std::find_if(s.begin(), s.end(), ::isdigit) != s.end();        

或者,如果你有的话:

return std::any_of(s.begin(), s.end(), ::isdigit);

【讨论】:

    【解决方案2】:

    C++11:

    #include <algorithm>
    #include <cctype>
    #include <string>
    #include <iostream>
    
    bool has_any_digits(const std::string& s)
    {
        return std::any_of(s.begin(), s.end(), ::isdigit);
    }
    
    int main()
    {
        std::string query("H311o, W0r1d!");
    
        std::cout << query << ": has digits: "
                  << std::boolalpha
                  << has_any_digits(query)
                  << std::endl;
        return 1;
    }
    

    输出:

    H311o, W0r1d!: has digits: true

    【讨论】:

    • 是否按原样为您编译?你没有收到类似this one的错误?
    • @BenjaminLindley 感谢您的尝试,我已经解决了。
    【解决方案3】:

    它总是返回false,因为你的逻辑是倒退的。您正在使用 || 运算符和 == npos 检查。如果字符串中缺少任何一个特定数字,== npos 的计算结果为true 并且满足||,因此您返回false。您需要使用!= npos 检查,然后如果任何检查评估为true,则返回true

    bool Person::contains_number(const std::string &c)
    {
        if (c.find('0') != std::string::npos ||
            c.find('1') != std::string::npos ||
            c.find('2') != std::string::npos ||
            c.find('3') != std::string::npos ||
            c.find('4') != std::string::npos ||
            c.find('5') != std::string::npos ||
            c.find('6') != std::string::npos ||
            c.find('7') != std::string::npos ||
            c.find('8') != std::string::npos ||
            c.find('9') != std::string::npos)
        {
            return true;
        }
    
        return false;
    }
    

    或者:

    bool Person::contains_number(const std::string &c)
    {
        return (
            c.find('0') != std::string::npos ||
            c.find('1') != std::string::npos ||
            c.find('2') != std::string::npos ||
            c.find('3') != std::string::npos ||
            c.find('4') != std::string::npos ||
            c.find('5') != std::string::npos ||
            c.find('6') != std::string::npos ||
            c.find('7') != std::string::npos ||
            c.find('8') != std::string::npos ||
            c.find('9') != std::string::npos
        );
    }
    

    更简单的解决方案是使用find_first_of() 而不是find()

    bool Person::contains_number(const std::string &c)
    {
        return (c.find_first_of("0123456789") != std::string::npos);
    }    
    

    【讨论】:

      【解决方案4】:

      How to test if a string contains any digits in C++

      应该这样做!

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

      【讨论】:

        【解决方案5】:

        您正在使用||(或运算符)来检查 if 语句中的多个条件。 如果表达式之一为真,or 运算符返回真(满足条件)。

        or 运算符首先计算其左侧的表达式:如果为真,则不计算其右侧的表达式并返回真。 如果左边的表达式为假,则计算右边的表达式并将其结果作为 || 的结果返回。运营商

        这是你的函数中发生的事情:

        • c 是否包含“0”?如果没有(因为 find() 中的 std::string::npos 表示未找到)则返回 false
        • c 是否包含“1”?如果不是,则返回 false
        • ...

        因此,将 or 运算符替换为 &&(和运算符)。

        【讨论】:

          猜你喜欢
          • 2013-11-20
          • 2016-11-11
          • 2022-06-30
          • 2015-02-07
          • 2010-12-19
          • 1970-01-01
          • 2011-03-23
          相关资源
          最近更新 更多