在 C++ 中检查输入是否为整数

Jinku Hu 2023年1月30日 2021年4月29日
  1. 使用 std::find_if 算法检查 C++ 中的输入是否为整数
  2. 在 C++ 中使用 std::string::find_first_not_of 函数来检查输入是否为整数
  3. 在 C++ 中使用 std::string::find_first_not_of 函数来检查输入是否为整数
在 C++ 中检查输入是否为整数

本文将演示如何在 C++ 中检查输入是否为整数的多种方法。

使用 std::find_if 算法检查 C++ 中的输入是否为整数

std::find_if<alogrithm> 头文件中定义的 STL 算法库的一部分,可用于搜索范围中的特定元素。由于用户输入很可能是字符串,因此我们将假定输入数据存储在 std::string 对象中。注意,我们实现了一个名为 isNumber 的函数,该函数引用了 std::string,并返回 bool 值。

在下面的示例中使用的 std::find_if 函数的原型带有三个参数,其中前两个参数指定范围元素-[first, last]。第三个参数是一个单数谓词,它是一个 lambda 函数,它通过计算 isdigit 函数的倒数值来返回 bool 值。在外层,将 std::find_if 返回值与 str.end() 进行比较,因为表达式的 true 值表示未找到非数字字符。因此,这是数字。此外,我们在逻辑上将前一个表达式与!str.empty 进行与运算,以指示该字符串为空,并返回 false

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::cin;
using std::endl; using std::string;

bool isNumber(const string& str)
{
    return !str.empty() &&
        std::find_if(str.begin(), str.end(),
            [](unsigned char c) { return !std::isdigit(c); }) == str.end();
}

int main(){
    string str1 = "12345.";
    string str2 = "12312";
    string str3 = "123142.2";

    isNumber(str1) ? cout << "Number\n" : cout << "Not number\n";
    isNumber(str2) ? cout << "Number\n" : cout << "Not number\n";
    isNumber(str3) ? cout << "Number\n" : cout << "Not number\n";

    exit(EXIT_SUCCESS);
}

输出:

Not number
Number
Not number

在 C++ 中使用 std::string::find_first_not_of 函数来检查输入是否为整数

另外,我们可以使用 std::string 对象内置的 find_first_not_of 方法重新实现 isNumber 函数。find_first_not_of 可以获取字符串值,并找到不等于字符串序列中所有字符的第一个字符。如果函数找不到这样的字符,则返回 string::npos。因此,我们指定所有 10 位小数作为 find_first_not_of 参数,并检查其与 npos 的相等性,因为表达式值是从函数返回的。

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::cin;
using std::endl; using std::string;

bool isNumber(const string& str)
{
    return str.find_first_not_of( "0123456789" ) == string::npos;
}

int main(){
    string str1 = "12345.";
    string str2 = "12312";
    string str3 = "123142.2";

    isNumber(str1) ? cout << "Number\n" : cout << "Not number\n";
    isNumber(str2) ? cout << "Number\n" : cout << "Not number\n";
    isNumber(str3) ? cout << "Number\n" : cout << "Not number\n";

    exit(EXIT_SUCCESS);
}

输出:

Not number
Number
Not number

在 C++ 中使用 std::string::find_first_not_of 函数来检查输入是否为整数

但是请注意,先前的方法不会识别实数,而是将其视为非法数字。所以,我们可以在字符串中加入 . 字符,让函数识别任何带有点符号的数字序列为有效数字。我们需要消除两种情况。字符是输入序列中的第一个和最后一个符号,按照我们的惯例,这不是有效的实数。我们可以使用 string 内置方法 frontback 来验证输入是否以点符号开头/结尾。最后,我们在逻辑上将所有三个表达式彼此进行运算并返回该值。

#include <iostream>
#include <string>
#include <algorithm>

using std::cout; using std::cin;
using std::endl; using std::string;

bool isNumber3(const string& str)
{
    return str.find_first_not_of( ".0123456789" ) == string::npos &&
        str.front() != '.' && str.back() != '.';
}

int main(){
    string str1 = "12345.";
    string str2 = "12312";
    string str3 = "123142.2";

    isNumber(str1) ? cout << "Number\n" : cout << "Not number\n";
    isNumber(str2) ? cout << "Number\n" : cout << "Not number\n";
    isNumber(str3) ? cout << "Number\n" : cout << "Not number\n";

    exit(EXIT_SUCCESS);
}

输出:

Not number
Number
Number
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相关文章 - C++ Integer