stringstream

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
#if 0//extract 1
    string name;
    int age;
    float weight;

    stringstream stream("mary 25 63.4");
    stream>>name>>age>>weight;
    cout<<"your information is "<<endl;
    cout<<name<<" "<<age<<" "<<weight<<endl;
#elif 0//extract 2
    string name, tmp;
    int age;
    float weight;
    stringstream stream;

    cout<<"input your name:"<<endl;
    cin>>tmp;
    stream<<tmp;

    stream>>name;


    cout<<"input your age:"<<endl;
    stream.clear();              /* if you want use the same one stream to convert,please invoke clear() before,or you will error
                                            void clear (iostate state = goodbit);
                                            Set error state flags
                                            Sets a new value for the stream's internal error state flags.
                                            The current value of the flags is overwritten: All bits are replaced by those in state; If state is goodbit (which is zero) all error flags are cleared.

                                            In the case that no stream buffer is associated with the stream when this function is called, the badbit flag is automatically set

                                            (no matter the value for that bit passed in argument state).

                                            Note that changing the state may throw an exception, depending on the latest settings passed to member exceptions.
                                            The current state can be obtained with member function rdstate.*/
    stream<<tmp;
    stream>>age;

    cout<<"input your weight:"<<endl;
    stream.clear();
    cin>>tmp;
    stream<<tmp;
    stream>>weight;

//    stream>>name>>age>>weight;//error ---all input string will be chained to one string
    cout<<"your information is "<<endl;
    cout<<name<<" "<<age<<" "<<weight<<endl;
#elif 1 //insert
    stringstream stream;
    string name;
    int age;
    float weight;
    cout<<"input your name:";
    cin>>name;
    cout<<"input your age:";
    cin>>age;
    cout<<"input your weight:";
    cin>>weight;
    stream<<"name:"<<name<<"\nage:"<<age<<"\nweight:"<<weight;
    cout<<stream.str()<<endl;
#endif


    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值