文字列がC++で数値であるかどうかを判別します
この投稿では、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>
指定された範囲内の要素を見つけるための述語を受け入れるヘッダー。次のように使用して、最初の数字以外の文字の位置を見つけることができます。文字列内のすべての文字が数字の場合、文字列は数字である必要があります。このソリューションは、正の整数に対してのみ機能します。
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
The 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文字列strを指定された基数の整数として解析し、変換が不可能な場合はゼロ値を返すC標準ライブラリ関数。解析に使用できます 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++で文字列が数値かどうかを判断できます。