stringstream过滤空格将字符串转化为整形的使用

转自: http://blog.csdn.net/u010126535/article/details/21036263

#include<cstdio>  
#include<cstring>  
#include<map>  
#include<vector>  
#include<cmath>  
#include<cstdlib>  
#include<stack>  
#include<queue>  
#include <iomanip>  
#include<sstream>   
#include<iostream>  
#include<algorithm>  
using namespace std ;  
/*  
因为有多行输入,且每行的输入数据个数不一,所以可以考虑运用#include<sstream> 中的stringstream 流  
现将每行输入存储在string s中,然后将s流入 stringstream ss中 ,然后从ss中依次按整形读取整数,存入整形数组中   
*/    
int main()  
{  
      int n,duan,chong;  
      int a[100000];  
      int Max=0 ,Min=9999999 ;  
      string s ;  
      scanf("%d",&n);  
      getchar();           
      while(n--)  
      {  
            getline(cin,s);  
            stringstream ss ;  
            ss << s ;                //注意流方向,把字符串输入ss流中   
            int x;       //注意从stringstream中解析对象的时候,是以空格和回车键为分隔符的  
            while(  ss >> x )       //ss流中字符串转换为int 流出来 ;   
            {  
                   a[x]++;  
                   if( x > Max)  Max=x;  
                   if( x < Min)  Min=x ;  
            }        
      }  
      for(int i = Min ; i <= Max ; i++ )  
      {  
           if(a[i]==0 && i!=Min)  
               duan=i ;  
          if(a[i]>1)  
              chong=i;  
      }  
     printf("%d %d\n",duan,chong);  
      
      return 0;  
}   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值