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;
}