跳转至

Streams

约 153 个字 15 行代码 预计阅读时间 1 分钟

Stream naming convections

Input Output Header
Generic istream ostream <iostream>
File ifstream ofstream <fstream>
C String istrstream ostrstream <strstream>
C++ String istringstream ostringstream <sstream>

Defining a stream extractor

C++
1
2
3
4
5
istream& operator>>(istream& is, T& obj)
{
    // specific code to read obj
    return is;
}

Defining a stream inserter

C++
1
2
3
4
5
ostream& operator<<(ostream& os, const T& obj)
{
    // specific code to write obj
    return os;
}

这里传入的 objconst 的,因为我们只需要读取它的值,而不需要修改它。

Other output operators

  • flush()
    • Force output of stream contents
    • 把 stream 对象中缓存的内容都清空

Formatting using manipulators

起状态控制的作用,需要 #include <iomanip> 头文件。

C++
1
2
3
int n;
cout << "Enter number in hexadecimal: " << endl;
cin >> hex >> n;

这里的 hex 是一个 manipulator,表示输入的数字是十六进制的。它会把输入的数字转换成十进制的整数。

Note

endl 也是一个 manipulator,它会在输出流中插入一个换行符,并 flush 。

Creating manipulators

C++
ostream& manip(ostream& os)
{
    // specific code to manipulate os
    return os;
}

ostream& tab(ostream& os)
{
    return out << "\t";
}

cout << "Hello" << tab << "World!" << endl;

评论