istringstream、ostringstream、stringstream 类介绍
来源:互联网 发布:数控镗铣床编程 编辑:程序博客网 时间:2024/05/22 03:11
转载自:http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html
关于istringstream、ostringstream、stringstream 类介绍 ,这篇文章写得比较好,
0、C++的输入输出分为三种:
(1)基于控制台的I/O
(2)基于文件的I/O
(3)基于字符串的I/O
1、头文件
[cpp] view plaincopyprint?
#include <sstream>
2、作用
istringstream类用于执行C++风格的字符串流的输入操作。
ostringstream类用于执行C++风格的字符串流的输出操作。
strstream类同时可以支持C++风格的串流的输入输出操作。
3、具体分析
istringstream类
描述:从流中提取数据,支持 >> 操作
这里字符串可以包括多个单词,单词之间使用空格分开
istringstream的构造函数原形: istringstream::istringstream(string str);
初始化:使用字符串进行初始化
istringstream istr("1 56.7"); istr.str("1 56.7");//把字符串"1 56.7"存入字符串流中
使用:我们可以使用分解点获取不同的数据,完成 字符串 到 其他类型 的转换
常用成员函数:
str():使istringstream对象返回一个string字符串
举例:把字符串类型的数据转换为其他类型
#include <iostream> #include <sstream> using namespace std; int main() { istringstream istr("1 56.7"); cout<<istr.str()<<endl;//直接输出字符串的数据 "1 56.7" string str = istr.str();//函数str()返回一个字符串 cout<<str<<endl; int n; double d; //以空格为界,把istringstream中数据取出,应进行类型转换 istr>>n;//第一个数为整型数据,输出1 istr>>d;//第二个数位浮点数,输出56.7 //假设换下存储类型 istr>>d;//istringstream第一个数要自动变成浮点型,输出仍为1 istr>>n;//istringstream第二个数要自动变成整型,有数字的阶段,输出为56 //测试输出 cout<<d<<endl; cout<<n<<endl; system("pause"); return 1; }
举例2:把一行字符串放入流中,单词以空格隔开。之后把一个个单词从流中依次读取到字符串
#include <iostream> #include <sstream> using namespace std; int main() { istringstream istr; string line,str; while (getline(cin,line))//从终端接收一行字符串,并放入字符串line中 { istr.str(line);//把line中的字符串存入字符串流中 while(istr >> str)//每次读取一个单词(以空格为界),存入str中 { cout<<str<<endl; } } system("pause"); return 1; }
输入:123 34 45
输出:
123 换行 34 换行 45
ostringstream类
描述:把其他类型的数据写入流(往流中写入数据),支持<<操作
ostringstream的构造函数原形: ostringstream::ostringstream(string str);
初始化:使用字符串进行初始化
ostringstream ostr("1234"); ostr.str("1234");//把字符串"1234"存入字符串流中
举例:
#include <iostream> #include <sstream> using namespace std; int main() { //初始化输出字符串流ostr ostringstream ostr("1234"); cout<<ostr.str()<<endl;//输出1234 ostr.put('5');//字符4顶替了1的位置 cout<<ostr.str()<<endl;//输出5234 ostr<<"67";//字符串67替代了23的位置,输出5674 string str = ostr.str(); cout<<str<<endl; system("pause"); return 1; }
stringstream类
描述:是对istringstream和ostringstream类的综合,支持<<, >>操作符,可以进行字符串到其它类型的快速转换
stringstream的构造函数原形如下: stringstream::stringstream(string str);
初始化:使用字符串进行初始化
stringstream str("1234"); str.str("1234");//把字符串"1234"存入字符串流中
作用:
1、stringstream通常是用来做数据转换的
2、将文件的所有数据一次性读入内存
举例1:基本数据类型变字符串
/*基本数据类型变字符串*/ #include <fstream> #include <iostream> #include <sstream> using namespace std; int main() { /*整型变字符串*/ int n = 10; string str; stringstream stream; stream << n; stream >> str; cout<<str<<endl; stream.clear();//多次使用stringstream,要先清空下,不能使用stream.str("");否则下面输出10 /*char* 变 string*/ char cStr[10] = "china"; stream << cStr; stream >> str; cout<<str<<endl; system("pause"); return 1; }
举例2:字符串变基本数据类型
/*字符串变基本数据类型*/ #include <fstream> #include <iostream> #include <sstream> using namespace std; int main() { /*字符串 变 double*/ double n; string str = "12.5"; stringstream stream; stream << str; stream >> n; cout<<n<<endl; stream.clear();//多次使用stringstream,要先清空下,不能使用stream.str(""); /*string 变 char* */ string str1 = "china"; char cStr[10]; stream << str1; stream >> cStr; cout<<cStr<<endl;//输出china system("pause"); return 1; }
注意:#include <iostream> #include <sstream> using namespace std; int main(int argc,char *argv[]) { std::stringstream stream; string str; while(1) { //clear(),这个名字让很多人想当然地认为它会清除流的内容。 //实际上,它并不清空任何内容,它只是重置了流的状态标志而已! stream.clear(); // 去掉下面这行注释,清空stringstream的缓冲,每次循环内存消耗将不再增加! //stream.str(""); stream<<"sdfsdfdsfsadfsdafsdfsdgsdgsdgsadgdsgsdagasdgsdagsadgsdgsgdsagsadgs"; stream>>str; //测试输出每次循环,你的内存消耗增加了多少! cout<<"Size of stream = "<<stream.str().length()<<endl; system("PAUSE"); } system("PAUSE"); return EXIT_SUCCESS; }由于stringstream构造函数会特别消耗内存,似乎不打算主动释放内存(或许是为了提高效率),但如果你要在程序中用同一个流,反复读写大量的数据,将会造成大量的内存消耗,因些这时候,需要适时地清除一下缓冲 (用 stream.str("") )。另外不要企图用 stream.str().resize(0),或 stream.str().clear() 来清除缓冲,使用它们似乎可以让stringstream的内存消耗不要增长得那么快,但仍然不能达到清除stringstream缓冲的效果,内存的消耗还在缓慢的增长!,至于stream.flush(),则根本就起不到任何作用。
0 0
- istringstream、ostringstream、stringstream 类介绍 .
- istringstream、ostringstream、stringstream 类介绍 .
- istringstream、ostringstream、stringstream 类介绍
- istringstream、ostringstream、stringstream 类介绍
- istringstream、ostringstream、stringstream 类介绍 .
- istringstream、ostringstream、stringstream 类介绍
- istringstream、ostringstream、stringstream 类介绍
- istringstream、ostringstream、stringstream 类介绍
- istringstream、ostringstream、stringstream 类介绍
- istringstream、ostringstream、stringstream 类介绍 .
- C++string流之istringstream,ostringstream,stringstream类介绍
- C\C++中istringstream、ostringstream、stringstream 类介绍 .
- ostringstream、istringstream、stringstream这三个类
- C++中的ostringstream、istringstream、stringstream类
- <sstream>—istringstream、ostringstream、stringstream类
- C++之istringstream、ostringstream、stringstream 类详解
- istringstream, ostringstream和stringstream
- C++ : ostringstream、istringstream、stringstream
- GlideCatchUtil
- POJ 3191
- Java 反射
- Android apk出现两个应用图标
- matlab利用hinge loss实现多分类SVM
- istringstream、ostringstream、stringstream 类介绍
- ListView 相关属性设置
- C++中inline关键字的作用
- 开始工作了,写第一篇博客
- 腾讯基础研究笔试题
- 一个对随机过程的理解
- Android中ListView中item的EditText控件监听剩余字数
- ducoment for pyvex VEXIR
- js时间戳解析