c++でstringタイプの文字列変数が数値であるかどうかをどのように判断しますか?

1074 ワード

#include 
#include 
#include 
using namespace std;
 
 
bool isnum(string s)
{
        stringstream sin(s);
        double t;
        char p;
        if(!(sin >> t))
        /*  :
            sin>>t   sin   double   (    int float      ),      ,    0,           0
        */
               return false;
        if(sin >> p)
        /*  :            ,           (  :34.f),       (sin>>t)               , stringstream             ,                     ,               ,       ,    .f   ,      ,  false;           ,   sin>>p  0,       else  
          */
                return false;
        else
                return true;
}
 
int main()
{
        string s;
        while(cin >> s)
        {
                if(isnum(s))
                        cout << s << " is a number." << endl;
                else
                        cout << s << " is not a number." << endl;
        }
}
  

原文リンク、原作者に感謝