I/O流类库(四)

字符串流


  • istringstream,由istream派生而来,提供读string的功能
  • ostringstream,由ostream派生而来,提供些string的功能
  • stringstream,由iostream派生而来,提供读写string的功能

istringstream

#include <iostream>
#include<sstream>

using namespace std;

int main(void)
{
    string line;
    string word;
    while (getline(cin, line))
    {
        istringstream iss(line);
        while (iss >> word)
            cout << word << "#";
        cout << endl;
    }
}

这里写图片描述

ostringstream

#include <iostream>
#include<sstream>

using namespace std;
string Double2Str(double& val)
{
    ostringstream oss;
    oss << val;
    return oss.str();
}
double Str2Double(const string& str)
{
    istringstream iss(str);
    double val;
    iss >> val;
    return val;
}
int main(void)
{
    double val = 55.55;
    string str = Double2Str(val);
    cout << str << endl;

    str = "123.123";
    val = Str2Double(str);
    cout << val << endl;
    return 0;
}

这里写图片描述

/*-------------------例-----------------------------*/
#include <iostream>
#include<sstream>

using namespace std;

int main(void)
{
    istringstream iss("192,168,0,100");
    cout << iss.str() << endl;
    int v1, v2, v3, v4;
    char ch;
    iss >> v1 >> ch >> v2 >> ch >> v3 >> ch >> v4;
    ostringstream oss;
    ch = '.';
    oss << v1 << ch << v2 << ch << v3 << ch << v4;
    cout << oss.str() << endl;
    return 0;
}

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值