sstream用法

sstream用法#include<sstream>stringstream对象用于输入一行字符串,以空格为分隔符把该行分隔开来stringstr=”helloworldIamveryhappy!”;stringstreamsstream(str);…

大家好,又见面了,我是你们的朋友全栈君。

#include<sstream>

stringstream 对象用于输入一行字符串,以  空格  为分隔符把该行分隔开来

    string str= "hello world I am very happy!";                           
    stringstream sstream(str);                                              //sstream<<
 
    while (sstream)
      {
        string substr;
 
        sstream>>substr;
        cout << substr << endl;    //也可vec.push_back(substr);
      } 

/*****************************************************类型转换******************************************************/

1.类型转换

1.1利用stringstream进行常见类型转换

sstream用法

/--------------------int转string------------------/
        stringstream stream;
        string result;
 
        int i = 1000;
 
        stream << i;                //将int输入流
        stream >> result;           //从stream中抽取前面插入的int值
        cout << result << endl;     // cout the string "1000"
 
 /--------------------int转char *------------------/
 
	 stringstream stream;
	 char result[8] ;
 
	 stream << 8888;             //向stream中插入8888
	 stream >> result;           //抽取stream中的值到result
	 cout << result <<endl;      // 屏幕显示 "8888"

1.2利用to_string() 进行常见类型转换, 返回值为string

s1=to_string(10.5);                        //double到string

s2=to_string(123);                        //int到string

1.3定义一个通用的转换模板,用于任意类型之间的转换

template<class out_type,class in_value>
out_type convert(const in_value & t)
{
    stringstream stream;
    stream<<t;//向流中传值
    out_type result;//这里存储转换结果
    stream>>result;//向result中写入值
    return result;
}
 
int main()
{
    double d;
    string salary;
    string s="12.56";
    d=convert<double>(s);//d等于12.56
    salary=convert<string>(9000.0);//salary等于”9000”    
    
    return 0;
}

/*********************************************stringstream补充************************************************/

2.stringstream补充

2.1重复利用stringstream对象

如果你打算在多次转换中使用同一个stringstream对象,记住再每次转换前要使用clear()方法;

在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。
 

    stringstream stream;
    int first, second;
 
    stream<< "456"; //插入字符串
    stream >> first; //转换成int
    cout << first << endl;
 
    stream.clear(); //在进行多次转换前,必须清除stream,否则第二个数据可能输出乱码
    
    stream << true; //插入bool值
    stream >> second; //提取出int
    cout << second << endl;

2.2选择性 读取stingstream 对象中的数据

string test = "-123 9.87 welcome to, 989, test!";
	stringstream strm(test);


	int i1 = 0;
	int i2 =0;
	float f1 = 0.0, f2 = 0.0;
	char c1,c2;
	char buff[1024];


	strm >> i1;
	cout << "读取int类型:" << i1 << endl;
	strm >> f1;
	cout << "读取float类型:" << f1 << endl;
	strm >> c1;
	cout << "读取char类型:" << c1 << endl;
	strm >> buff;
	cout << "读取buffer类型:" << buff << endl;
	
	//1.如果遇到一个字符值等于第二个参数,那么就停止ignore()
	//2.如果ignore100个字符之后还没遇到值等于第二参数的字符,也得停止ignore() 
	//因此100是ignore()所能忽略的最大字符数。此时忽略了“to,”
	strm.ignore(100, ',');

	strm >> buff;
	cout << "读取buffer类型:" << buff << endl;


	//读取的数据类型不匹配
	strm >> i2;
	cout << "第二次 读取int类型:" << i2 << endl;
	strm >> f2;
	cout << "第二次 读取float类型:" << f2 << endl;
	strm >> c2;
	cout << "第二次 读取char类型:" << c2 << endl;

	strm >> buff;
	cout << "读取buffer类型:" << buff << endl;

sstream用法

 

string test = "-123 9.87 welcome to, 989, test!";
	stringstream strm(test);


	int i1 = 0;
	int i2 =0;
	float f1 = 0.0, f2 = 0.0;
	char c1,c2;
	char buff[1024];


	strm >> i1;
	cout << "读取int类型:" << i1 << endl;
	strm >> f1;
	cout << "读取float类型:" << f1 << endl;
	strm >> c1;
	cout << "读取char类型:" << c1 << endl;
	strm >> buff;
	cout << "读取buffer类型:" << buff << endl;
	
	//1.如果遇到一个字符值等于第二个参数,那么就停止ignore()
	//2.如果ignore100个字符之后还没遇到值等于第二参数的字符,也得停止ignore() 
	//因此100是ignore()所能忽略的最大字符数。此时忽略了“to,”
	strm.ignore(100, ',');

	strm >> buff;
	cout << "读取buffer类型:" << buff << endl;
	strm >> buff;
	cout << "读取buffer类型:" << buff << endl;

	//当把sstream流读空之后,后面读取都会失败,故输出的是初始值
	strm >> i2;
	cout << "第二次 读取int类型:" << i2 << endl;
	strm >> f2;
	cout << "第二次 读取float类型:" << f2 << endl;
	strm >> c2;
	cout << "第二次 读取char类型:" << c2 << endl;

sstream用法

 

string test = "-123 9.87 welcome to, 989, test!";
	stringstream strm(test);


	int i1 = 0;
	int i2 =0;
	float f1 = 0.0, f2 = 0.0;
	char c1,c2;
	char buff[1024];


	strm >> i1;
	cout << "读取int类型:" << i1 << endl;
	strm >> f1;
	cout << "读取float类型:" << f1 << endl;
	strm >> c1;
	cout << "读取char类型:" << c1 << endl;
	strm >> buff;
	cout << "读取buffer类型:" << buff << endl;
	
	//1.如果遇到一个字符值等于第二个参数,那么就停止ignore()
	//2.如果ignore100个字符之后还没遇到值等于第二参数的字符,也得停止ignore() 
	//因此100是ignore()所能忽略的最大字符数。此时忽略了“to,”
	strm.ignore(100, ',');

	strm >> buff;
	cout << "读取buffer类型:" << buff << endl;
	strm >> buff;
	cout << "读取buffer类型:" << buff << endl;

	//当把sstream流读空之后,后面读取都会失败,故输出的是初始值
	if (strm >> i2) {
		cout << "第二次 读取int类型:" << i2 << endl;
	}
	
	if (strm >> f2) {
		cout << "第二次 读取float类型:" << f2 << endl;
	}
	
	if (strm >> c2) {
		cout << "第二次 读取char类型:" << c2 << endl;
	}

sstream用法

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/135881.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PhpStorm 2021.4.11 x64激活码【在线破解激活】

    PhpStorm 2021.4.11 x64激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    47
  • 网页视频下载方法[通俗易懂]

    问题有时候我们在做PPT或者撰写一些报告、案例的时候,需要一些视频作为素材,网上搜到后,想下载却比较麻烦,有的在专业视频网站上,有的在新闻网站上,有的在机构网站上,有的在社交媒体上,有没有简便、快速、可行的视频下载方法,并且不需要付费或者安装额外软件呢。下面说明几种方法,基本可以涵盖绝大多数情况。解决办法非专业视频网站上的视频以下两种办法需要使用谷歌浏览器Chrome电脑版打开视频所在的网页,右键——>审查元素——>点击左上角的小箭头——>在页面中选中视频界面——>在审查

    2022年4月8日
    86
  • Java String「建议收藏」

    Java String「建议收藏」Java String

    2022年4月20日
    41
  • 微型计算机原理与接口技术课程代码,微型计算机原理与接口技术

    微型计算机原理与接口技术课程代码,微型计算机原理与接口技术课程代码:02205教材名称:微型计算机原理与接口技术学分:6分主编:徐骏善、朱岩出版社:机械工业出版社版次:2014年版开本:16开定价:36.00元适用专业A080306机电一体化工程教材简介:本书是全国高等教育自学考试机电一体化工程专业(专科)课指定教材,按照2014年新修订的该课程自学考试大纲编写。本书内容包括两部分:C语言程序设计,讲述C语…

    2022年10月2日
    0
  • 搭建nexus3私库简要步骤

    搭建nexus3私库简要步骤搭建nexus私库简要步骤:安装nexus登录nexus页面端默认地址http://loaclhost:8081登录nexus账号默认admin/admin123maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jarmaven-releases:私库发行版jar,初次安装请将Deploymentpolicy设置为Allowredeploymaven-snapshots:私库快照(调试版本)jarm

    2022年7月18日
    14
  • pytest重试_pytest失败重跑

    pytest重试_pytest失败重跑安装:pip3installpytest-rerunfailures重新运行所有失败用例要重新运行所有测试失败的用例,请使用–reruns命令行选项,并指定要运行测试的最大次数:$py

    2022年7月29日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号