C++判断string是不是数字

  1. #include <iostream>  
  2. #include <sstream>  
  3. using namespace std;  
  4.   
  5. bool isNum(string str);  
  6. int main( )  
  7. {  
  8.   
  9.     string ss1="2y5r";  
  10.     string ss2="2558";  
  11.     if(isNum(ss1))  
  12.     {  
  13.         cout<<"ss1 is a num"<<endl;  
  14.     }  
  15.     else{  
  16.         cout<<"ss1 is not a num"<<endl;  
  17.   
  18.     }  
  19.     if(isNum(ss2))  
  20.     {  
  21.         cout<<"ss2 is a num"<<endl;  
  22.     }  
  23.     else{  
  24.         cout<<"ss2 is not a num"<<endl;  
  25.           
  26.     }  
  27.     return 0;  
  28. }  
  29.   
  30. bool isNum(string str)  
  31. {  
  32.     stringstream sin(str);  
  33.     double d;  
  34.     char c;  
  35.     if(!(sin >> d))  
  36.         return false;  
  37.     if (sin >> c)  
  38.         return false;  
  39.     return true;  
  40. }  

输出结果:

ss1 is not a num

ss2 is a num

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值