[C++] Stream

发布于 26 天前  65 次阅读

Please refresh the page if equations are not rendered correctly.

1. Explanation of endl and >> operators

1.1 endl

endl is a function looks like the one below:

ostream& endl(ostream& os)
    os.put(os.widen('\n')); // Insert a new line character
    os.flush(); // Flush the stream
    return os;

endl is a common manipulator used in C++ for formatted output. The standard endl is a part of the C++ Standard Library and doesn't need to be defined by the user.

The example function endl takes a reference to an ostream object as its argument. The ostream class is a part of the C++ Standard Library and provides functions for output to streams (like files or the console).

Inside the function, os.put(os.widen('\n')) is called to insert a new line character into the stream. The put function writes a single character to the stream, and os.widen('\n') converts the newline character to the appropriate format for the stream.

Then, os.flush() is called to flush the stream. Flushing the stream means that any buffered output is written out immediately. This is often done when you want to make sure that all output has been written, for example before closing a file or when you want to display output immediately on the console.

Finally, the function returns the ostream object it received as its argument. This allows for chaining of output operations. For example, you can write cout << "Hello" << endl; and both the string "Hello" and the newline will be written to cout.

1.2 <<

ostream& ostream::operator<<(ostream& (*func)(ostream&))
    return func(*this);

This function is an overload of the operator<< for ostream. The operator<< is commonly used for output to streams in C++. This example of operator<< takes a function pointer as its argument. The function pointed to must take a reference to an ostream object as its argument and return a reference to an ostream object. This is the type of function that can be used as a manipulator with operator<<. The standard endl function is an example of such a function.

Inside the function, func(*this) is called. This calls the function pointed to by func with the current object (*this) as its argument. The result of this call (which should be a reference to an ostream object) is then returned.

This allows you to write code like cout << endl;. In this case, endl is a function that takes and returns an ostream&, so it can be used with operator<< thanks to this overload.

However, it seems like you're getting a compilation error. The error message suggests that there's a conflict with another operator<< overload. This could be because the ostream class already has an operator<< overload for this type of function in the C++ Standard Library, and you're not allowed to redefine it. If you're trying to define this function inside the ostream class, you should remove it because it's already defined in the library. If you're trying to define it outside the class, you should make sure it's not conflicting with the existing overload.

1.3 Example scripts


using namespace std;

int main()

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

cout << ends; // Insert a null character (\0) and flush the stream
cout << flush; // Flush the stream

cin >> ws; // Extract and discard white spaces from the input stream

// 'std::setfill' is defined in header '<iomanip>'
cout << setw(8) << left << setfill('_') << 99 << endl; // setfill('_') with single quotes

cout << hex << showbase << 14; // 0xe

return 0;


最后更新于 2024-03-20