检查字符串中是否包含数字的函数

6

我正在进行一个c++项目(我刚开始学习), 但是我不明白为什么这个函数不起作用。 我试图编写一个"Person"类,其中包含一个变量first_name,并使用函数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;
    }

5个回答

22

将所有的||替换为&&

更好的做法是:

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

或者,如果你已经有了它:

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

11

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!: 有数字: true


你这样编译能通过吗?你没有像这个一样的错误吧? - Benjamin Lindley

7
它总是返回 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);
}    

6

1

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

or运算符首先评估其左侧的表达式:如果该表达式为真,则不评估其右侧的表达式并返回true。如果左侧的表达式为false,则评估右侧的表达式,并将其结果作为||运算符的结果返回。

这是您的函数中发生的情况:

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

因此,请使用&&(and运算符)替换or运算符。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接