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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Python字符串操作之字符串分割与组合「建议收藏」

    Python字符串操作之字符串分割与组合「建议收藏」12、字符串的分割和组合str.split():字符串分割函数通过指定分隔符对字符串进行切片,并返回分割后的字符串列表。语法:os.path.split():路径文件分割函数join(seq):将序列组合成字符串函数

    2022年5月29日
    90
  • latex缩进与对齐_latex 换行缩进

    latex缩进与对齐_latex 换行缩进LATEX模板(中国运筹学会年会论文模板)%%Paper…关键词位于摘要下方,行首不缩进。摘要使用小五号(…以上这些词后均不换行。中文关键词之间以中文分号……2基础知识4Latex讲义1.单词之间用一个或多个空格分开.多个空格和一个空格效果相同.2.换行:生成的文件会自动换行,在tex文件中用一个回车换行……3.LaTeX在使用体验方面…

    2022年5月14日
    95
  • 去除字符串中的双引号「建议收藏」

    去除字符串中的双引号「建议收藏」str为“123”如果一个双引号:str1=str.replace(“\””,””).replace(“\””,””);如果不确定有多少个双引号:str2=str.replace(/\”/g,””);此方法为替换,也可用于去除制定字符,如:Stringstr=”12/3″;str1=str.replace(“\/”,””);str2=str.replace(/\//g,

    2022年5月23日
    48
  • Linux初识之Kali Linux 系统安装详细教程(虚拟机)[通俗易懂]

    Linux初识之Kali Linux 系统安装详细教程(虚拟机)[通俗易懂]文章出自个人博客https://knightyun.github.io/2018/04/15/kali-linux-install,转载请申明目录一、KaliLinux介绍1、Linux2、Kali二、虚拟机安装与配置1、下载2、安装配置三、Kali系统安装与配置一、KaliLinux介绍1、Linux引用一下百度百科:Li…

    2022年5月18日
    29
  • PHP 获得当前页面所有变量常量的值

    PHP 获得当前页面所有变量常量的值

    2021年9月25日
    46
  • pycharm安装opencv-python_pycharm opencv

    pycharm安装opencv-python_pycharm opencv使用pip安装:pipinstallpython-opencv在网络距离美国较远的情况下,或PIP版本未达到要求的情况下,会install失败。解决方法1.重试2.科学方法,这里说明使用科学方法的端口问题,切记不要和本地计算机使用同一个端口,容易造成Pycharm未知错误。3.手动去官网下载文件这里有两种类型的手动安装文件setup.pyXXXX.whl以上两种文件都可以通过pycharm的命令行工具直接cd到目录进行直接安装。方法:1pyth

    2022年8月27日
    3

发表回复

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

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