HDU 1088 (模拟)使用<sstream>模版库
来源:互联网 发布:英剧推荐知乎 编辑:程序博客网 时间:2024/06/05 19:06
#include <sstream>
你的编译器支持<sstream>吗?
<sstream>库是最近才被列入C++标准的。(不要把<sstream>与标准发布前被删掉的<strstream>弄混了。)因此,老一点的编译器,如GCC2.95,并不支持它。如果你恰好正在使用这样的编译器而又想使用<sstream>的话,就要先对它进行升级更新。
<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。简单起见,我主要以stringstream为中心,因为每个转换都要涉及到输入和输出操作。
注意,<sstream>使用string对象来代替字符数组。这样可以避免缓冲区溢出的危险。而且,传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险。
string到int的转换
string result=”10000”;
int n=0;
stream<<result;
stream>>n;//n等于10000
重复利用stringstream对象
如果你打算在多次转换中使用同一个stringstream对象,记住再每次转换前要使用clear()方法;
在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。
代码参考了别人的,然后自己学习了下stringstream:CODE:
/* *problem ID: HDU 1088 *Author ID: fuqiang11 *TIME: 2013-07-16 15:47:10 */#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <sstream> //添加头文件 using namespace std;string br = "--------------------------------------------------------------------------------";int main(){string line;int count = 0;while(getline(cin,line)){stringstream stream(line); //初始化line为stringstream类型 string word; //接受一个一个的单词 while(stream>>word) //从stringstream流输入到word {//cout<<count;if( word == "<br>" ){count = 0;cout<<endl;continue;}else if( word == "<hr>"){if(count != 0)cout<<endl<<br<<endl;elsecout<<br<<endl;count = 0;continue;}if(count == 0){count = word.size();cout<<word;continue;}else if(count + word.size() + 1 > 80){count = word.size(); //sizecout<<endl<<word;continue;}count += (word.size() + 1);cout<<" "<<word;}}cout<<endl;}关于stringtream的简单实用:
#include <sstream>#include <iostream> int main(){ std::stringstream stream; char result[8] ; stream << 8888; //向stream中插入8888 stream >> result; //抽取stream中的值到result std::cout << result << std::endl; // 屏幕显示 "8888"}
关于清空内容:
.clear()是清空标志位 .str() 是清内容.str("")才是真正的清内容,采用断点调试,stringstream stream;//只申明什么都不做的执行结果和stringstream stream;stream<<"sfsfs";stream.str("");执行结果是一样的是一样的(断点后看结果都是有很多的“0X00000‘指向错误的指针’”)
- HDU 1088 (模拟)使用<sstream>模版库
- sstream与模版函数
- sstream使用范例
- 读取PPM文件源代码(使用了fstream, sstream,string)
- 浅析sstream库
- 浅析sstream库
- sstream标准库
- sstream库 用法
- C++标准库<sstream>
- 浅析sstream库
- sstream
- sstream
- sstream
- sstream 与 fstream的使用
- sscanf和sstream的使用
- hdu 1402 (FFT 模版)
- 使用宏模拟C++模版完成简单数据类型的交换
- 字符串输入输出流(<sstream>)
- Linux 输入(input)子系统架构分析
- 别人画表象,我画内在——新锐100设计师林晨专访
- 当前session id
- Qt5.0.2 + MinGW编译出来的程序发布
- 比较好的资料共享
- HDU 1088 (模拟)使用<sstream>模版库
- sql高效率分页
- 代理模式
- ORACLE备份恢复
- Solr学习笔记四--solrj的使用(更新文档)
- Java中switch语句
- hdu1088
- ssh login without passwd
- 可以直接拿来用的15个jQuery代码片段