9.7 字符串流
来源:互联网 发布:淘宝 html5 编辑:程序博客网 时间:2024/05/16 17:47
字符流概念:字符串(string)也可以看作字符流。可以用输入输出操作来完成串流的操作。串流与内存相关,所以也称内存流。
串流类包括ostrstream、istrstream、strstream,它们在<strstrea.h>中说明。串流类对象可以保存字符,也可以保存整数、浮点数。串流类对象采用文本方式。
其构造函数常用下面几个:
istrstream::istrstream(const char * str);
istrstream::istrstream(const char * str,int);
ostrstream::ostrstream(char *,int,int=ios::out);
strstream::strstream(char *,int,int);
其中第二个参数说明数组大小,第三参数为文件打开方式。
【例9.12】字符串流类的应用。
#include<strstream>
#include<iostream>
#include<cstring>
using namespace std;
int main(){
int i;
char str[36]="This is a book.";
char ch;
istrstream input(str,36); //以串流为信息源
ostrstream output(str,36);
cout<<"字符串长度:"<<strlen(str)<<endl;
for(i=0;i<36;i++){
input>>ch; //从输入设备(串)读入一个字符,所有空白字符全跳过
cout<<ch; //输出字符
}
cout<<endl;
int inum1=93,inum2;
double fnum1=89.5,fnum2;
output<<inum1<<' '<<fnum1<<'\0'; //加空格分隔数字
cout<<"字符串长度:"<<strlen(str)<<endl;
istrstream input1(str,0); //第二参数为0时,表示连接到以串结束符终结的串
input1>>inum2>>fnum2;
cout<<"整数:"<<inum2<<'\t'<<"浮点数:"<<fnum2<<endl; //输出:整数:93 浮点数:89.5
cout<<"字符串长度:"<<strlen(str)<<endl;
return 0;
}
串流类包括ostrstream、istrstream、strstream,它们在<strstrea.h>中说明。串流类对象可以保存字符,也可以保存整数、浮点数。串流类对象采用文本方式。
其构造函数常用下面几个:
istrstream::istrstream(const char * str);
istrstream::istrstream(const char * str,int);
ostrstream::ostrstream(char *,int,int=ios::out);
strstream::strstream(char *,int,int);
其中第二个参数说明数组大小,第三参数为文件打开方式。
【例9.12】字符串流类的应用。
#include<strstream>
#include<iostream>
#include<cstring>
using namespace std;
int main(){
int i;
char str[36]="This is a book.";
char ch;
istrstream input(str,36); //以串流为信息源
ostrstream output(str,36);
cout<<"字符串长度:"<<strlen(str)<<endl;
for(i=0;i<36;i++){
input>>ch; //从输入设备(串)读入一个字符,所有空白字符全跳过
cout<<ch; //输出字符
}
cout<<endl;
int inum1=93,inum2;
double fnum1=89.5,fnum2;
output<<inum1<<' '<<fnum1<<'\0'; //加空格分隔数字
cout<<"字符串长度:"<<strlen(str)<<endl;
istrstream input1(str,0); //第二参数为0时,表示连接到以串结束符终结的串
input1>>inum2>>fnum2;
cout<<"整数:"<<inum2<<'\t'<<"浮点数:"<<fnum2<<endl; //输出:整数:93 浮点数:89.5
cout<<"字符串长度:"<<strlen(str)<<endl;
return 0;
}
0 0
- 9.7 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- C++字符串流
- 字符串流stringstream
- 字符串流的使用
- 字符串流题目
- 数组、字符串流
- 字符串流的用法
- C++ 字符串流
- stringstream 字符串流
- 数据结构和算法C语言实现:使用链表实现稀疏多项式
- 长期更新 Java语法小黑屋
- 28个Java常用的工具类
- 多线程编程(1):线程的基本用法
- Winform文件下载之断点续传
- 9.7 字符串流
- 下载Windows Embedded CE6.0
- java常用工具方法2
- Linux释放内存
- 9.8 文件与对象
- 继承
- CMake 入门实战
- Python编码整理
- 寻章摘句,感悟。