C++ istringstream用法

istringstream可以将输入的字符串转换为字符串流,可以用于分割字符串.
需要包含头文件件:#include

用法实例如下:

/*********************************************************
/  Module			: app
/--------------------------------------------------------
/  FuncDescription	: 演示istringstream分割字符串的方法
/  BasicKnoledge    : istringstream用法
/  Blog             : 
*********************************************************/

#include <iostream>
#include <string>
#include <sstream>

using namespace std;
int main()
{
    //分割空格分开的字符串
    string strDevideByBlank = "aa bbb cccc";
    istringstream sin(strDevideByBlank);  //将str转换为istringstream(字符串流)对象

    string word;
    while(sin >> word)     //输入字符串,遇到空格时结束
    {
        cout << word << "\t";
    }
    cout << endl;

    //分开任意符号分开的字符串
    string strDevideByColon = "there:is:a:lot:to:learn";
    string strDevideByStar = "but*time*is*always*not*enough";
    string lastWord = "test";
    istringstream sin1(strDevideByColon);
    
    //getline读取成功返回1,失败返回0
    while(getline(sin1, word, ':'))
    {
        cout << word << "\t";
    }
    cout << endl;

    istringstream sin2(strDevideByStar);
    while(getline(sin2, word, '*'))
    {
        cout << word << "\t";
    }
    cout << endl;

    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值