1.头文件:#include<sstream>

2.stringstream是C++提供的串流(stream)物件,其中:

clear()重置流的标志状态;str()清空流的内存缓冲,重复使用内存消耗不再增加!

在使用stringstream时遇到的问题:

 
#include <cstdlib>
#include <iostream>
#include <sstream> using namespace std; int main(int argc, char * argv[])
{
stringstream stream;
int a,b; stream<<"80";
stream>>a; stream<<"90";
stream>>b; cout<<a<<endl;
cout<<b<<endl; system("PAUSE ");
return EXIT_SUCCESS;
}
 

运行结果:

预期b为90,但是出现-858993460,这是由于stringstream重复使用时,没有清空导致的。

修改之后:

 
#include <cstdlib>
#include <iostream>
#include <sstream> using namespace std; int main(int argc, char * argv[])
{
stringstream stream;
int a,b; stream<<"80";
stream>>a; stream.clear(); stream<<"90";
stream>>b; cout<<a<<endl;
cout<<b<<endl; system("PAUSE ");
return EXIT_SUCCESS;
}
 

运行结果:

但是clear()仅仅清空标志位,并没有释放内存。

 
#include <cstdlib>
#include <iostream>
#include <sstream> using namespace std; int main(int argc, char * argv[])
{
stringstream stream;
int a,b; stream<<"80";
stream>>a; stream.clear(); cout<<"Size of stream = "<<stream.str().length()<<endl; stream<<"90";
stream>>b; cout<<"Size of stream = "<<stream.str().length()<<endl; cout<<a<<endl;
cout<<b<<endl; system("PAUSE ");
return EXIT_SUCCESS;
}
 

clear()之后,虽然结果正确了,但是stream占用的内存却没有释放!在实际的应用中,要是多次使用stringstream,每次都增加占用的内存。

可以利用stringstream.str("")来清空stringstream。

void str ( const string & s );   // copies the content of string s to the string object associated with the string stream buffer. The function effectivelly calls rdbuf()->str(). Notice that setting a new string does not clear the error flags currently set in the stream object unless the member function clearis explicitly called.

 
#include <cstdlib>
#include <iostream>
#include <sstream> using namespace std; int main(int argc, char * argv[])
{
stringstream stream;
int a,b; stream<<"80";
stream>>a;
cout<<"Size of stream = "<<stream.str().length()<<endl; stream.clear();
stream.str(""); cout<<"Size of stream = "<<stream.str().length()<<endl; stream<<"90";
stream>>b; cout<<"Size of stream = "<<stream.str().length()<<endl; cout<<a<<endl;
cout<<b<<endl; system("PAUSE ");
return EXIT_SUCCESS;
}
 

运行结果:

stringstream默认空格会直接分词!

題目:输入的第一行有一个数字 N 代表接下來有 N 行数字,每一行数字里有不固定个数的整数,打印每一行的总和。

输入:

3
1 2 3
20 17 23 54 77 60
111 222 333 444 555 666 777 888 999

输出:

6
251
4995

 
string s;
stringstream ss;
int n, i, sum, a;
cin >> n;
getline(cin, s); // 换行读取
for (i=0; i<n; i++)
{
getline(cin, s);
ss.clear();
ss.str(s);
sum=0;
while (1)
{
ss >> a;
if ( ss.fail() )
break;
sum+=a;
}
cout << sum << endl;
}
 

本文主要介绍 C++ 中 stringstream 类的常见用法。

1 概述

<sstream> 定义了三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作。本文以 stringstream 为主,介绍流的输入和输出操作。

<sstream> 主要用来进行数据类型转换,由于 <sstream> 使用 string 对象来代替字符数组(snprintf方式),就避免缓冲区溢出的危险;而且,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。简单说,相比c库的数据类型转换而言,<sstream> 更加安全、自动和直接。

2 代码示例

2.1 数据类型转换

这里展示一个代码示例,该示例介绍了将 int 类型转换为 string 类型的过程。示例代码(stringstream_test1.cpp)如下:

#include <string>
#include <sstream>
#include <iostream>
#include <stdio.h> using namespace std; int main()
{
stringstream sstream;
string strResult;
int nValue = ; // 将int类型的值放入输入流中
sstream << nValue;
// 从sstream中抽取前面插入的int类型的值,赋给string类型
sstream >> strResult; cout << "[cout]strResult is: " << strResult << endl;
printf("[printf]strResult is: %s\n", strResult.c_str()); return ;
}

编译并执行上述代码,结果如下:

2.2 多个字符串拼接

本示例介绍在 stringstream 中存放多个字符串,实现多个字符串拼接的目的(其实完全可以使用 string 类实现),同时,介绍 stringstream 的清空方法。

示例代码(stringstream_test2.cpp)如下:

#include <string>
#include <sstream>
#include <iostream> using namespace std; int main()
{
stringstream sstream; // 将多个字符串放入 sstream 中
sstream << "first" << " " << "string,";
sstream << " second string";
cout << "strResult is: " << sstream.str() << endl; // 清空 sstream
sstream.str("");
sstream << "third string";
cout << "After clear, strResult is: " << sstream.str() << endl; return ;
}

编译并执行上述代码,结果如下:

从上述代码执行结果能够知道:

  • 可以使用 str() 方法,将 stringstream 类型转换为 string 类型;
  • 可以将多个字符串放入 stringstream 中,实现字符串的拼接目的;
  • 如果想清空 stringstream,必须使用 sstream.str(""); 方式;clear() 方法适用于进行多次数据类型转换的场景。详见示例2.3。

2.3 stringstream的清空

清空 stringstream 有两种方法:clear() 方法以及 str("") 方法,这两种方法有不同的使用场景。str("") 方法的使用场景,在上面的示例中已经介绍了,这里介绍 clear() 方法的使用场景。示例代码(stringstream_test3.cpp)如下:

#include <sstream>
#include <iostream> using namespace std; int main()
{
stringstream sstream;
int first, second; // 插入字符串
sstream << "";
// 转换为int类型
sstream >> first;
cout << first << endl; // 在进行多次类型转换前,必须先运行clear()
sstream.clear(); // 插入bool值
sstream << true;
// 转换为int类型
sstream >> second;
cout << second << endl; return ;
}

编译并执行上述代码,结果如下:

注意:在本示例涉及的场景下(多次数据类型转换),必须使用 clear() 方法清空 stringstream,不使用 clear() 方法或使用 str("") 方法,都不能得到数据类型转换的正确结果。下图分别是未使用 clear() 方法、使用 str("") 方法时的运行结果:

https://blog.csdn.net/fengbingchun/article/details/51287210

stringstream用法的更多相关文章

  1. C++ stringstream用法(转)

    一直觉得C++ iostream的cout输出比起printf差了太多,今天查c++字符串拼接的时候偶然看到原来还有stringstream这个类,还是挺好用的,该类位于<sstream> ...

  2. [转]string和stringstream用法总结

    转自:http://blog.csdn.net/xw20084898/article/details/21939811 作者:xw20084898 一.string string 是 C++ 提供的字 ...

  3. stringstream 用法

    stringstream stringstream 是 C++ 提供的另一个字串型的串流(stream)物件,和之前学过的 iostream.fstream 有类似的操作方式.要使用 stringst ...

  4. string和stringstream用法总结

    参考:http://blog.csdn.net/xw20084898/article/details/21939811

  5. STL用法总结

    stringstream用法:对已有的运算符赋予多重含义,使同一个运算符作用于不同类型的数据导致不同类型的行为. stream << i   将i输入流中 stream >> ...

  6. c++之stringstream类的用法

    简介: 今天利用opecv提取每一帧图片并保存到本地指定目录下的时,对于保存的每一帧的图片希望第几帧体现在图片名中, 这里便用到了stringstream类的将数字转化为字符串这一功能 C++ Str ...

  7. sstream头文件-getline 函数 和 stringstream函数 和string的常见用法

    2017-08-12 19:50:50 writer:pprp getline函数可以读入一行的字符,不论有没有空格 第一个参数,流 第二个参数 ,将流读入的地方 第三个参数,当读到某个字符的时候停止 ...

  8. stringstream的用法

    stringstream的基本用法 stringstream是字符串流.它将流与存储在内存中的string对象绑定起来. 在多种数据类型之间实现自动格式化. 1.stringstream对象的使用 # ...

  9. stringstream类的简介和用法

    一.简介 <sstream>类库定义了三种类:istringstream,ostringstream,stringstream.分别用来进行流的输入,流的输出,输入输出操作.在此演示str ...

随机推荐

  1. 163data.com.cn data

    163data.com.cn是什么?终于搞清楚了...   查看文章     163data.com.cn是什么?终于搞清楚了... 2008-05-31 00:41 一场误会,真TN的无聊的吓人从日 ...

  2. Mybatis控制台打印SQL语句的两种方式

    问题描述在使用mybatis进行开发的时候,由于可以动态拼接sql,这样大大方便了我们.但是也有一定的问题,当我们动态sql拼接的块很多的时候,我们要想从*mapper.xml中直接找出完整的sql就 ...

  3. stm32自带的flash分布图

    缘由是要用到flash来保存数据,因此查阅了数据手册与参考手册,一般情况下,将要保存的数据存放到比较靠后的地方,page254,page255,4k字节,已经相当多的了,

  4. Django --- ORM表查询

    目录 使用数据库之前的配置工作 单表操作常用的方法 一对多字段的增删改查 多对多字段数据的增删改查 跨表查询 聚合函数 分组查询 F与Q查询 使用数据库之前的配置工作 settings.py中的配置 ...

  5. IDEA 中tomcat图片储存和访问虚拟路径(图片和程序分家)

    本文链接:https://blog.csdn.net/qq_36481052/article/details/78813213 **前段时间,遇到了图片已经储存了文件中也显示有图片,但就是死活访问不到 ...

  6. Java实现数组元素反转

    package com.fgy.demo; /** * 数组元素反转 */ public class demo05 { public static void main(String[] args) { ...

  7. Restful API 指南

    作为软件开发人员,我们大多数人在日常生活中使用或构建 REST api.API 是系统之间的默认通信方式.亚马逊是如何有效地使用 api 进行通信的最佳例子. 在这篇文章中,我将讨论如何更好地设计 R ...

  8. contextMenuEvent

    #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDesktopWidget&g ...

  9. WebAPI学习

    WebAPI概述 今天的web计算平台包含了广泛的功能,其中的大部分均可以通过API(应用程序编程接口)访问. web平台归为6个基本设施,都会用到webapi,包括存储服务.消息服务.计算服务.信息 ...

  10. Django+element ui前后端不分离的博客程序

    最近把去年写的一个烂尾博客(使用了django1.11和element ui)又重新完善了一下,修改了样式和增加了新功能 github链接:https://github.com/ngauerh/Nag ...