(original) C++primer (fifth edition)--1.2 initial input and output

Source: Internet
Author: User

The C + + language does not define any input/output (IO) statements and, instead, includes a comprehensive standard library to provide the IO mechanism.

Many of the examples in this book use the iostream Library. The iostream library contains two basic types, istream and ostream, representing the input and output streams, respectively. A stream is a sequence of characters that is read out or written to an IO device from an IO device. The term "stream" wants to express that, over time, the characters are in order to survive or consume.

  Standard input and Output objects

  1. cin(pronounced see-in): standard input ; IStream type of object;

2. cout(pronunciation see-out): standard output ; ostream type of object;

3. cerr(pronunciation see-err): standard error ; Ostream type object; typically used to output warnings and error messages;

4. Clog(hard See-log): Ostream type Object; The general message used to output the program runtime;

The system usually prizes the windows that the program runs to associate with these objects. So when we read CIN, the data is read from the program's running window, and when we write data to Cout,cerr, and clog, we write to the same window.

  A program that uses the IO library

Using the IO Library, extend the main program so that it can prompt the user to enter two numbers and then output their and:

#include <iostream>#include<cstdlib>intMain () {std::cout<<"Enter The numbers:"<<Std::endl; intV1 =0, V2 =0; Std::cin>> v1 >>v2; Std::cout<<"The sum of"<< v1 <<" and"<<v2<<" is"<< V1 + v2 <<Std::endl; System ("Pause");//the star month uses vs 2012, so the output window needs to be paused    return 0; }

The output results are as follows:

The first line of the program

#include <iostream>

Tell the editor that we want to use the iostream library. The name in the angle brackets indicates a header file. Each program that uses the standard library facility must contain the associated header file. #include指令必须出现在所有函数之外. We generally place all # include directives for a program at the beginning of the source file.

  Writing data to a stream

The first statement of the main function body executes an expression . In C + +, an expression produces a computed result that consists of one or more operands and (usually) an operator. The expression in this statement uses the output operator (<<) to print the message on the standard output:

" Enter The numbers: " << Std::endl;

The << operator accepts two operands: the left operand must be an Ostream object, and the operand on the right is the value to be printed. The output operator evaluates to its left operand.

We used two times the << operator. Because this operator returns the operand to its left, the result of the first operator becomes the left operand of the second operator. In this way, we can connect the output request together. Therefore, our expression is equivalent to:

" Enter The numbers: " ) << Std::endl;

The left operand of each operator in the chain is the same, in this case std::cout. We can also generate the same output with two statements:

" Enter The numbers: "  << Std::endl;

The first output operator prints a message to the user. This message is a string literal constant (string literal), which is a string surrounded by double quotation marks. The text between the double quotation marks is printed to the standard output.

The second operator prints Endl, a special value called the manipulator (manipulator).

The Endl function is to end the current row and brush the contents of the buffer associated with the device to the device. The buffered flush operation guarantees that all output produced by the program so far is actually written to the output stream, rather than just waiting in memory for the write stream. That is, using standard output, only the content that is going to be output is written to memory, and it is not written to the output stream immediately, but instead waits in memory until the buffer is flushed to write the stream of characters in memory into the output stream.

Note:    programmers often add print statements while debugging. Such statements should be guaranteed to "always" flush the stream. Otherwise, if the program crashes, the output may remain in the buffer, causing incorrect inference about where the program crashed.

  Use a name from the standard library

Attentive readers may notice that the program uses std::cout and Std::endl rather than direct cout and Endl.

(Little white: really yes.) Why Std. )

(Star Month: cough.) Xiao Bai, you are not home for the New Year!!! )

(Little white: ...) )

(Star Moon: we continue.) The prefix std:: Indicates that the name cout and Endl are defined in the namespace named std (namespace).

(Small white: What does a namespace do?? )

Well, assuming there's no namespace, let's take a look at the test code:

// because of the test, the code rigor is not guaranteed #include <iostream>voidint  a)    {string  "This istest! " }int  main () { int a = 3;
ABS (a); // --1 return 0;
}

In the above code, the ABS method is called at 1. Assuming there is no namespace , we know that the system has its own ABS method, then at 1 we are calling the system method, or our own definition of the method? For this ambiguity, C + + provides the concept of a namespace.

Namespaces: can help us avoid inadvertent name definition conflicts, and use the same names in the library to cause conflicts.

All names defined by the standard library are in the namespace Std.

Add the namespace after the above code:

//due to testing, there is no guarantee of the rigor of the Code#include <iostream>voidAbsinta) {stringstr ="This is test!"}intMain () {intA =3;  ABS (a); //using a custom functionStd::abs (a);//using the functions inside the standard library    return 0;}

Using a standard library through a namespace has a side effect: When you use a name in a standard library, you must display a description that we want to use from the name in the namespace Std.

We use the scope operator (::) to indicate that we want to use--the name MMM defined in the namespace XXX. For example: Std::cout,:: Indicate--Use the name cout in the namespace Std.

(original) C++primer (fifth edition)--1.2 initial input and output

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.