這篇文章將討論如何確定 C++ 中的字符串是否為數字。解決方案應檢查字符串是否包含數字序列。
1. 使用循環
一個簡單的解決方案是遍歷字符串,直到遇到非數字字符。如果所有字符都是數字,則字符串是數字。此解決方案不適用於負數或浮點數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> bool isNumeric(std::string const &str) { auto it = str.begin(); while (it != str.end() && std::isdigit(*it)) { it++; } return !str.empty() && it == str.end(); } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
2.使用 std::find_if
我們也可以使用標準算法 std::find_if
來自 <algorithm>
header,它接受一個謂詞來查找指定範圍內的元素。可以如下使用它來查找第一個非數字字符的位置。如果字符串中的所有字符都是數字,則字符串必須是數字。此解決方案僅適用於正整數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> #include <algorithm> bool isNumeric(std::string const &str) { auto it = std::find_if(str.begin(), str.end(), [](char const &c) { return !std::isdigit(c); }); return !str.empty() && it == str.end(); } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
3.使用 string::find_first_not_of
這 string::find_first_not_of 函數在字符串中搜索與任何指定字符都不匹配的第一個字符。可以如下使用它來確定字符串是否為數字。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> bool isNumeric(std::string const &str) { return !str.empty() && str.find_first_not_of("0123456789") == std::string::npos; } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
4.使用 std::all_of
從 C++11 開始,我們可以使用 std::all_of
返回的函數 true
如果指定的謂詞適用於指定範圍內的所有元素。可以如下使用它來確定字符串是否為數字。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> bool isNumeric(std::string const &str) { return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit); } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
5.使用 strtol()
功能
或者,我們可以使用 strtol() C 標準庫函數,將 C 字符串 str 解析為指定基數中的整數,如果無法進行轉換,則返回零值。它可以用來解析一個 std::string
並判斷字符串是否為數字,如下所示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> bool isNumeric(std::string const &str) { char* p; strtol(str.c_str(), &p, 10); return *p == 0; } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
6. 使用升壓
如果您已經在使用 boost 庫,則可以使用解析 C++ 字符串 boost::lexical_cast<>()
功能。它拋出一個 boost::bad_lexical_cast
如果無法進行轉換,則例外。這也適用於負整數和浮點數。這是完整的代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> #include <string> #include <boost/lexical_cast.hpp> bool isInteger(std::string const &str) { try { boost::lexical_cast<int>(str); return true; } catch(boost::bad_lexical_cast &e) { return false; } } bool isDouble(std::string const &str) { try { boost::lexical_cast<double>(str); return true; } catch(boost::bad_lexical_cast& e) { return false; } } int main() { std::string str = "100"; std::cout << std::boolalpha << isInteger(str) << std::endl; // true return 0; } |
7. 使用正則表達式
最後,我們可以使用正則表達式來確定字符串是否為數字。以下解決方案目前適用於正整數和負整數。它可以很容易地擴展到處理浮點數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <regex> bool isNumeric(std::string const &str) { return std::regex_match(str, std::regex("[(-|+)|][0-9]+")); // "[(-|+)|][[:digit:]]+" } int main() { std::string str = "100"; std::cout << std::boolalpha << isNumeric(str) << std::endl; // true return 0; } |
這就是確定字符串在 C++ 中是否為數字的全部內容。