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


相关推荐

  • 人工智能:智能优化算法

    人工智能:智能优化算法**人工智能:智能优化算法优化问题是指在满足一定条件下,在众多方案或参数值中寻找最优方案或参数值,以使得某个或多个功能指标达到最优,或使系统的某些性能指标达到最大值或最小值。优化问题广泛地存在于信号处理、图像处理、生产调度、任务分配、模式识别、自动控制和机械设计等众多领域。优化方法是一种以数学为基础,用于求解各种优化问题的应用技术。各种优化方法在上述领域得到了广泛应用,并且已经产生了巨大的经济效益和社会效益。实践证明,通过优化方法,能够提高系统效率,降低能耗,合理地利用资源,并且随着处理对象规模的增加

    2022年5月22日
    62
  • PotPlayer+SVP4视频补帧简易教程

    PotPlayer+SVP4视频补帧简易教程这两天突然想到之前看到过视频补帧的测试,印象中效果挺好。昨天试了试,也找过很多找教程,搜集了一些经验,这里先讲解一种特别简单的方法,再说一种不太简单但也很容易上手的方法(这个才是重点!!!)。要用到的软件:Potplayer–依照你系統位数(现在基本都是64位的),不清楚就裝32bitSVP4Free-依照Potplayer安裝版本進行安裝接下来是安装:1.Potpla…

    2022年7月12日
    26
  • java中beanutils_java bean

    java中beanutils_java beanBeanUtils<!–原型设计模式:复制属性–> Maven包<dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.9.3</version>

    2025年9月12日
    4
  • 单例模式(Singleton)应用场景和优缺点

    单例模式(Singleton)应用场景和优缺点单例(Singleton)模式 也叫单态模式概述:单例(Singleton)模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点。这就提出了一个问题:如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例?客户程序在调用某一个类时,它是不会考虑这个类是否只能有一个实例等问题的,所以,这应该是类设计者的责任,而不是类使用者的责任。 从另一个角度来说,Singleton模式其实也是一…

    2022年6月13日
    35
  • Nginx修改默认端口80

    Nginx修改默认端口80前言    安装流程请参考我的文章–Windows下安装Nginx。    博客地址:https://blog.csdn.net/zengwende/article/details/86610692修改步骤1、打开Nginx的配置文件nginx.conf2、修改默认端口的值即可(nginx默认的端口为80) …

    2022年9月8日
    1
  • vim中翻页的命令

    vim中翻页的命令整页翻页ctrl-fctrl-bf就是forwordb就是backward翻半页ctrl-dctlr-ud=downu=up滚一行ctrl-ectrl-yzz让光标所杂的行居屏幕中央zt让光标所杂的行居屏幕最上一行t=topzb让光标所杂的行居屏幕最下一行b=bottom转载于:https://www.cnblogs.com/orez88/articles/1867879….

    2022年6月2日
    51

发表回复

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

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