如何在 C++ 中確定一個字串是否是數字

Jinku Hu 2023年10月12日
  1. 使用 std::isdigit 方法來確定一個字串是否是一個數字
  2. 使用 std::isdigitstd::rangles::all_of 來確定一個字串是否是一個數字
  3. 使用 find_first_not_of 方法確定一個字串是否是數字
如何在 C++ 中確定一個字串是否是數字

本文介紹瞭如何檢查給定的 C++ 字串是否是數字。在我們深入研究之前,需要注意的是,以下方法只與單位元組字串和十進位制整數相容。

使用 std::isdigit 方法來確定一個字串是否是一個數字

第一個版本可能是實現解決方案的最明顯方式。也就是說,把一個字串作為引數傳給函式 isNumber,它遍歷字串中的每一個字元,並用 isdigit 方法檢查。當發現第一個非數字時,函式返回 false,如果沒有發現則返回 true。

#include <iostream>

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

bool isNumber(const string& str) {
  for (char const& c : str) {
    if (std::isdigit(c) == 0) return false;
  }
  return true;
}

int main() {
  string str1 = "231524randstr23";
  string str2 = "23152423";
  string str3 = "a3152423";

  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";

  return EXIT_SUCCESS;
}

輸出:

Not number
Number
Not number

請注意,我們通過 ? :三元條件運算子來輸出每個字串的判決,這是 if-else 的一個簡明變體。

使用 std::isdigitstd::rangles::all_of 來確定一個字串是否是一個數字

前面的方法對於強大的 C++ 來說是很基礎的,所以我們使用 C++20 的方法 std::ranges::all_of 和一些 lambda 表示式來實現一個更雄辯的解決方案。在我們的例子中,ranges::all_of 檢查指定的 lambda 是否對給定範圍 s.begin(), s.end() 中的每個元素都返回 true,如果條件滿足則返回 true。

#include <algorithm>
#include <iostream>

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

bool isNumber(const string& s) {
  return std::ranges::all_of(s.begin(), s.end(),
                             [](char c) { return isdigit(c) != 0; });
}

int main() {
  string str1 = "231524randstr23";
  string str2 = "23152423";
  string str3 = "a3152423";

  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";

  return EXIT_SUCCESS;
}

使用 find_first_not_of 方法確定一個字串是否是數字

這個版本使用了一個內建的字串搜尋演算法。該演算法搜尋與作為引數傳遞的字串中沒有的字元相同的第一個字元(在我們的例子中-"0123456789")。如果沒有找到這個字元,則返回 string::npos,因此我們返回 isNumber 的比較結果。

#include <iostream>

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

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

int main() {
  string str1 = "231524randstr23";
  string str2 = "23152423";
  string str3 = "a3152423";

  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";

  return EXIT_SUCCESS;
}
作者: 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++ String