C/C++中字符串流有什么用

本文主要介绍"C/C++中字符串流有什么用",希望能够解决您遇到有关问题,下面我们一起来看这篇 "C/C++中字符串流有什么用" 文章。

概述

文件流类和字符串流类都是 ostream, istream 和 iostream 类的派生类, 因此对它们的操作方法是基本相同的.

C/C++中字符串流有什么用

字符串流


文件流字符串流
概念文件流是以外存文件为输入输出对象的数据流字符串流也 称为内存流, 以内存中用户定义的字符数组 (字符串) 为输入输出的对象
相关流类ifstream, ofstream 和 fstreamstrstream
头文件ifstream, ofstream 和 fstreamstrstream

文件流类和字符串流类都是 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);

C/C++中字符串流有什么用

输入字符串流对象

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

C/C++中字符串流有什么用

字符数组 vs 文件

输出时数据不是流向外存文件, 而是流向内存中的一个存储空间. 输入时从内存中的存储空间读取数据.

C/C++中字符串流有什么用

字符串流对象关联的不是文件, 而是内存中的一个字符数组. 因此不需要打开和关闭文件.

每个文件的最后都有一个文件结束符, 表示文件的结束. 而字符流所关联的字符数组中没有相应的结束标志. 用户要指定一个特殊字符 ends('\0') 作为结束符, 在向字符数组写入全部数据后要写入此字符.

关于 "C/C++中字符串流有什么用" 就介绍到这。希望大家多多支持编程宝库

C语言中如何使用qsort函数:本文主要介绍"C语言中怎么使用qsort函数",希望能够解决您遇到有关问题,下面我们一起来看这篇 "C语言中怎么使用qsort函数" 文章。一.qsort函数是什么我们可以使用  搜索库函数网址或者MSDN软 ...