c++字符串流
来源:互联网 发布:阿里云 发邮件 编辑:程序博客网 时间:2024/06/05 07:28
字符串流
与文件流不同的是,字符串流是以内存中用户定义的字符串或字符数组为输入输出对象,也称为内存流
字符数组
可以存放字符,整数,浮点型及其他类型
1.再向字符数组存入数据前,要先将数据从二进制转换为ASCII代码,然后存放在缓冲区,再从缓冲区存放到字符数组。
2.从字符数组读数据时,先将字符数组中的ASCII数据送到缓冲区,再赋值给变量前先要将ASCII代码转换成二进制形式
1.建立输出字符串流对象
ostrstream::ostrstream(char*buffer,int n,int mode=ios::out);
buffer是指字符数组首元素的指针,n未指定流缓冲区的大小,第三个参数可选,默认为ios::out
2.建立输入字符串流对象
istrstream::istrstream(char*buffer);istrstream::istrstream(char*buffer,int n);
3.建立输入输出字符串流对象
ststream::ststream(char*buffer,int n,int mode);
下面实现在一个字符数组中存放了10个整数,以空格间隔,要求他们存放在数组中,并排序,再存回字符数组中。
#include<strstream>#include<iostream>using namespace std;int main(){char c[50]="3 -9 -4 9 1 2 28 23 45 4";float a[10];cout<<c<<endl;//打印字符串/************************************************/istrstream strin(c,sizeof(c));//建立输入串流对象strin并于c关联int i;for(i=0;i<10;i++)//从字符串读数据给a { strin>>a[i]; } for(i=0;i<10;i++)//打印a { cout<<a[i]<<" "; } cout<<endl;/**********************排序**************************/int j;for(i=0;i<10;i++){ for(j=i+1;j<10;j++) { if(a[j]>a[i]) swap(a[i],a[j]); }} for(i=0;i<10;i++) { cout<<a[i]<<" "; }/************************************************/ostrstream strout(c,sizeof(c));//建立输出流对象与c关联for(i=0;i<10;i++) { strout<<a[i]<<" "; } strout<<'\0';//加入结束符cout<<endl<<c<<endl;//打印c/************************************************/return 0;}
结果是
阅读全文
0 0
- C/C++格式化字符串流
- C/C++的字符串流
- 关于字符串流的学习(c++)
- C++primer 字符串流操作 p258
- [日常学习][C++]字符串流 <sstream>
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- 字符串流
- [C++]字符串流 istringstream 和 ostringstream 的用法
- 【day0411 C++】字符串流istringstream和ostringstream的用法
- C++字符串流
- 字符串流stringstream
- core核心模块--使用OpenCV对两幅图像求和(求混合(blending))
- 定时任务
- LeetCode Trie树(前缀树)
- Java8 重新认识HashMap
- 利用词间语义先验关系提升词嵌入表示
- c++字符串流
- SDUT 3916 上色的纱雾
- 运算符重载(2)———流运算符重载函数
- LeetCode 17. Letter Combinations of a Phone Number 递归回溯法
- C#的Socket编程
- Javascript 鼠标点击冒泡特效
- Cocos2d 使用shader创建圆角图片,以及添加投影
- 使用RTL-SDR和Matlab Simulink玩转软件无线电(一)
- ubuntu14.04安装spyder