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


相关推荐

  • mysql日志文件位置_linux怎么导出日志文件

    mysql日志文件位置_linux怎么导出日志文件登录mysql终端mysql-uroot-p输入密码:进入mysql>1.日志文件路径mysql>showvariableslike‘general_log_file’;±—————–±———————————–+|Variable_name|Value|±—————–±———————————–+|general_log_f

    2022年8月31日
  • 01分布两点分布_两点分布和01分布的区别

    01分布两点分布_两点分布和01分布的区别#–*–coding:utf-8–*–importmatplotlib.pyplotasplt#随机变量x只能取0,1我们称X服从以P为参数的(0-1)分布或两点分布p=

    2022年8月5日
  • Codeforces 455B A Lot of Games(字典树+博弈)[通俗易懂]

    Codeforces 455B A Lot of Games(字典树+博弈)

    2022年1月26日
  • 电阻参数_关于电阻的相关参数

    实际应用时,通常采用平均电阻温度系数,定义式:TCR(平均)=(R2-R1)/R1(T2-T1)有负温度系数、正温度系数及在某一特定温度下电阻只会发生突变的临界温度系数。紫铜的电阻温度系数为1/234.5℃。不同类型电阻温度稳定性从优到次,依次为:金属箔、线绕、金属膜、金属氧化膜、碳膜、有机实芯。1。镀金并不是为了减小电阻,而是因为金的化学性质非常稳定,不容易氧化,接头上镀金是为了防止接触不良(不…

    2022年4月8日
  • Spring它不支持依赖注入static静态变量

    Spring它不支持依赖注入static静态变量

    2022年1月1日
  • MATLAB GUI图形界面设计一个学生管理系统

    MATLAB GUI图形界面设计一个学生管理系统设计一个简单的学生成绩管理程序,包含如下功能:1、可创建不少于100名学生成员;2、每名成员的记录包括:学号、姓名、专业和5门课程的成绩; 3、能够实现添加、删除、修改学生成员;(增加非法字符警告)4、能够实现添加、删除、修改学生成员的课程成绩;(增加非法字符警告)5、分别计算每一门课程的平均成绩;6、每一门课程单独排序,输出由高到低的课程、姓名、成绩信息。7、提供图形界面实现上述功能,界面包含 系统登录界面 添加、删除、修改界面 被选中学生信息显示界面(基本信息,课程成绩,平均成

    2022年5月25日

发表回复

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

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