About COUT

zhaozj2021-02-16  89

Like most friends, I have encountered COUT to see the first C program - classic "Hello, World!", One of the best programs I have prepared by I have prepared (^ _ ^), It is probably like this: #include using namespace std; int main () {cout << "Hello, World!" << endl; return 0;} Due to the previous class C, the other Part is "normal", but Cout is very unique: neither function, it is not a special grammatical "statement" like IF, FOR, specified by C . Since it is just a preliminary introduction, then the book is just a simple COUT is a "standard input output" object in C ... this is really a very deep term. This hasn't finished, and then I met CIN ... I don't know if I don't know how to use them. I have been sincere and fearful. I want to escape from the concise printf (), after all, I can say: I am calling a function. That has a long skewers <<, >>'s gain, what is going on? I always want to treat them as a keyword, but it is not, and actually uses C language "to do", huh! But printf () starts with more people, I started to criticize my programs "C language traces" ... Later, as learned, finally understand the ghost tricks of COUT / CIN / CERR / ...: Those East East is not a "kidney", in fact, it is a function call, but this function is somewhat special, and it is an operator overload, which is exactly (the following is also COUT as an example) is overloaded "<<" Operator. We now let it show your functionality, please see the equivalent versions of Hello World! ("Include using namespace std; int main () {cout.operator << (" Hello, World! "); Cout.operator << (endl); return 0;} Compile operation, the result is no longer in the same way. The above program should be more likely to understand: COUT is an object of an iostream class, which has a member operator function Operator <<, will output Dongdong to the output device (generally on the screen) each time the call. Well, here there is a question: Why is the function operator << can accept different types of data, such as integer, floating point, strings, even pointers, etc. I think you have already guess it now, it's right, it is overloaded with operators. The operator function is basically no different from the general function, which can be overloaded any.

Standard library designers have already customized iostream :: Operator << For the overloaded version of various C basic data types, this has enabled our beginners to enjoy Cout << "Hello, World ! "<< Endl; simple - etc, this line is connected by two <<" Hello, World "and" endl "operator, then our second Edition Hello, World! It seems to be written : Cout.operator << ("Hello, World!"). Operator << (endl); "strong equivalent". Can you write this way? Confirm to the compiler ... OK, No quoblem! Well, we have basically seen the essence of COUT, now you may wish to move your hands, you will realize a simplified version of a COUT (Lite), in order to distinguish, we name the cout object named by our design, Myout object belongs Myoutstream. What we have to do is to overload a series of different types of Operator << operators for MyoutStream classes, simply, here we only have overloaded for integer (int) and string type (char *).

In order to indicate the relationship with the iostream, we no longer use the header file iostream, and the Printf function in the old stdio is used in the old STDIO. The program is simple, including the complete main function, all as follows: #include // in C And some old C is stdio.h, the new standard In order to distinguish the standard library //'s header file and the user's header file, it is recommended to use the version without extension //, for the original C library, no extension The header file name should be added to CCLASS myoutstream {public: const myoutstream & operator << (int value) const; / / overloaded const myoutstream & operator << (char * STR) const; // to string Overload}; const myoutstream & myoutstream :: Operator << (int value) const {printf ("% d", value); return * this; // Note this return ...} const myoutstream & myoutstream :: operator << (charr * Str) const {printf ("% s", str); return * this; //, here, here, pay attention to ...} myoutstream myout; //, anytime, anywhere, the global object to our service myout int main () {int A = 2003; char * mystr = "Hello, World!"; myout << mystr << a << "/ n"; return 0;} Our already Myout has begun to work, you can work for us.

The comments in the program indicate that we should pay special attention to the two: ie the Operator << function is executed, always returns a reference to its own, the output has been completed, why do you want more? I still remember that a little strange cout.operator << ("Hello, World!"). Operator << (endl)? It can achieve we can write COUT << "Hello, World! << Endl; not COUT <<" Hello, World! "; Cout << Endl; why can it connect? We analyze: According to the order of execution, the system first calls Cout.operator << ("Hello, World!"), Then? Then Cout.operator << will return it itself, that is, the last line of the function will appear similar to Return * this, so Cout.operator << ("Hello, World!") Is returned to Cout Then, then it is followed by .Operator << (endl), which is equivalent to cout.operator << (endl) - then will carry out the next output, if there are many << operators, calling Will it be going on ... Wow, is it very clever? Now you understand our myoutstream :: Operator << The last row of the mystery! Note that the most exciting line in the main function: myout << mystr << a << "/ n"; we know that the last "/ n" can achieve a wrap, but we are tutorial when using C Always intentionally, let us use endl, both seem to be the same - what is the mystery? In the book, the book says ENDL is a manipulator, which not only implements the wrap operation, but also refreshes the output buffer. What does that mean? It turns out that the data is not immediately transmitted to the output device, but the first buffer is entered, and when the appropriate timing (such as the device is idle), it can be incorporated by the buffer, or forced to refresh by the manipulator Flush: Cout << "Hello, World!" << "Flush The screen !!!" << flush; so when the program executes to operator << (flash), there is a possibility that the string data in front is still in the buffer Not displayed on the screen, but after executing Operator << (flash), the program will force all the data of the buffer to the output device and empty it. And the manipulator ENDL is equivalent to the shorter version of << "/ n" << Flush, which first outputs a newline character, and then realize the refresh of the buffer. It is probably because the general output is ended in the end of the wrap, and the end is a habit of refreshing. It is convenient to combine the two into ENDL. If the reader is interested, go back to implement a similar MyoutStream to our MyoutStream, which is related to the refresh C function is FFLUSH.

转载请注明原文地址:https://www.9cbs.com/read-13888.html

New Post(0)