sstream.h的使用 以一个计算时间的程序为例子

sstream是c++引入流的概念,让各种数据转换起来容易 主要使用方法

sstream ss;

int number;

string s="123";

ss>>s;

ss<<number;


在使用使要注意在此使用使需要清空流中的数据  使用str("")以及clear()



#include <stdio.h>

#include <iostream>

#include <math.h>

#include <string>

#include <sstream>

usingnamespace std;

struct travel_time

{

    int hours;

    int mins;

};

travel_time sum(travel_time t1,travel_time t2)

{

    travel_time t3;

    int tt3 =fabs(t1.hours*60+t1.mins-(t2.hours*60+t2.mins));

    t3.hours=tt3/60;t3.mins=tt3%60;

       return t3;

}


void show_time(travel_time t)

{

    cout<<t.hours<<"hs "<<t.mins<<"mins"<<endl;

}

int main(){

    string s ,a,b;

    stringstream s1,s2;

    cout<<"Enter t1(2-30):";

    cin>>s;

    int count =0;

    for(int i=0;i<s.length();i++)

    {

        if(s[i]>='0'&&s[i]<='9'&&count==0)

             a+=s[i];

        if(s[i]=='-')

             count++;

        if(count!=0)

        {

            if(s[i]>='0'&&s[i]<='9')

               b+=s[i];

        }

    }

    

    travel_time t1,t2{1,50};

    s1<<a;

    s2<<b;

    

    s1>>t1.hours;

    s2>>t1.mins;

    show_time(sum(t1, t2));

    return0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值