小编给大家分享一下C/C++中字符串流有什么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
文件流类和字符串流类都是 ostream, istream 和 iostream 类的派生类, 因此对它们的操作方法是基本相同的.
文件流 | 字符串流 | |
概念 | 文件流是以外存文件为输入输出对象的数据流 | 字符串流也 称为内存流, 以内存中用户定义的字符数组 (字符串) 为输入输出的对象 |
相关流类 | ifstream, ofstream 和 fstream | strstream |
头文件 | ifstream, ofstream 和 fstream | strstream |
文件流类和字符串流类都是 ostream, istream 和 iostream 类的派生类, 因此对它们的操作方法是基本相同的.
我们是输入是字符串形式, 存放在缓冲区内. 在数据内部是以二进制的方式表示的. 所以输出也是字符串形式的, 存储在输出缓冲区中.
#include <iostream>
using namespace std;
int main() {
double m, n;
char op;
cin >> m >> op >> n;
cout << m << " " << n << " " << op;
return 0;
}
输出结果:
123.45 + 6789.10
123.45 6789.1 +
字符串流类没有open
成员函数, 通过调用构造函数建立字符串流对象.
ostream 类的构造函数的原型:
ostrstream::ostrstream(char *buffer, int n, int mode=ios::out);
buffer 是指向字符数组首元素的指针
n 为指定的缓冲区的大小 (一般选与字符数组的大小相同)
mode 指操作方式, 默认为ios::out
方式
建立输出字符串流对象并与字符数组建立关联:
char ch2[20];
ostrstream strout(ch2, 20);
istrstream 类的两个带参的构造函数, 原型为:
istrstream::istrstream(char *buffer);
istrstream::istrstream(char *buffer, int n);
buffer 是指向字符数组首元素的指针, 用它来初始化流对象
n 是缓冲区大小, 可以用字符数组中的一部分
建立输入字符串流对象:
char ch3[40];
istrstream strin(ch3); // 将字符数组ch3中的全部数据作为输入字符串流的内容
istrstream strin(ch3,20); // 只将字符数组ch3中的前20个字符作为输入字符串流的内容
strstream 类提供的构造函数的原型为:
strstream::strstream(char *buffer, int n, int mode);
buffer 是指向字符数组首元素的指针
n 为指定的缓冲区的大小 (一般选与字符数组的大小相同)
mode 指操作方式, 默认为ios::out
方式
举个栗子:
char ch4[80];
strstream strio(ch4, sizeof(ch4), ios::in|ios::out);
写字符数组:
#include <iostream>
#include <strstream>
#include "Student.h"
using namespace std;
int main( )
{
// 定义数组
Student stud[3]= {
{1001, "Little"},
{1002, "Mid"},
{1003, "Big"},
};
char c[50]; // 定义char数组存放字符流
ostrstream strout1(c, 30);
for(int i = 0; i < 3; i++)
strout1 << stud[i].id << stud[i].name;
strout1 << ends; // ends是C++的I/O操作符,插入一个′\0′
cout << "array c:" << c << endl;
ostrstream strout2(c, 40);
for(int i = 0; i < 3; i++)
strout2 << stud[i].id << " " << stud[i].name << " ";
strout2 << ends;
cout << "array c:" << c << endl;
return 0;
}
输出结果:
array c:1001Little1002Mid1003Big
array c:1001 Little 1002 Mid 1003 Big
以字符串流为中介交换数据:
#include <iostream>
#include <strstream>
using namespace std;
int* bubble_sort(int array[10]);
void display(int array[10]);
int main() {
// 定义数组
char c[50] = "23 45 56 -23 -32 33 61 99 88 77";
int a[10], *pt;
// 输入字符串流
cout << "array c: " << c << endl;
istrstream strin(c, sizeof(c));
for (int i = 0; i < 10; i++) {
strin >> a[i];
}
// 调试输出
cout << "array a: ";
display(a);
cout << endl;
// 对数组 a 排序进行冒泡排序
pt = bubble_sort(a);
// 输出字符串流
ostrstream strout(c, sizeof(c));
for (int i = 0; i < 10; ++i) {
strout << *(pt+i) << " ";
}
cout << "array c: " << c << endl;
return 0;
}
输出结果:
array c: 23 45 56 -23 -32 33 61 99 88 77
array a: 23 45 56 -23 -32 33 61 99 88 77
array c: -32 -23 23 33 45 56 61 77 88 99
输出时数据不是流向外存文件, 而是流向内存中的一个存储空间. 输入时从内存中的存储空间读取数据.
字符串流对象关联的不是文件, 而是内存中的一个字符数组. 因此不需要打开和关闭文件.
每个文件的最后都有一个文件结束符, 表示文件的结束. 而字符流所关联的字符数组中没有相应的结束标志. 用户要指定一个特殊字符 ends('\0') 作为结束符, 在向字符数组写入全部数据后要写入此字符.
以上是“C/C++中字符串流有什么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。