设为首页 加入收藏

TOP

1.2 A First Look at Input/Output (2)
2013-10-07 16:14:50 来源: 作者: 【 】 浏览:105
Tags:1.2 First Look Input/Output

Writing to a Stream

The first statement in the body of main executes an expression. In C++(www.cppentry.com) an expression yields a result and is composed of one or more operands and (usually) an operator. The expressions in this statement use the output operator (the . operator) to print a message on the standard output:

  1. std::cout << "Enter two numbers:" << std::endl;  

The << operator takes two operands: The left-hand operand must be an ostream object; the right-hand operand is a value to print. The operator writes the given value on the given ostream. The result of the output operator is its left-hand operand. That is, the result is the ostream on which we wrote the given value.

Our output statement uses the << operator twice. Because the operator returns its left-hand operand, the result of the first operator becomes the left-hand operand of the second. As a result, we can chain together output requests. Thus, our expression is equivalent to

  1. (std::cout << "Enter two numbers:") << std::endl;  

Each operator in the chain has the same object as its left-hand operand, in this case std::cout. Alternatively,we can generate the same output using two statements:

  1. std::cout << "Enter two numbers:";   
  2. std::cout << std::endl;  

The first output operator prints a message to the user. That message is a string literal, which is a sequence of characters enclosed in double quotation marks. The text between the quotation marks is printed to the standard output.

The second operator prints endl, which is a special value called a manipulator. Writing endl has the effect of ending the current line and flushing the buffer associated with that device. Flushing the buffer ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.

Programmers often add print statements during debugging. Such statements should always flush the stream. Otherwise, if the programcrashes, output may be left in the buffer, leading to incorrect inferences about where the program crashed.

Using Names from the Standard Library
Careful readers will note that this program uses std::cout and std::endl rather than just cout and endl. The prefix std:: indicates that the names cout and endl are defined inside the namespace named std. Namespaces allow us to avoid inadvertent collisions between the names we define and uses of those same names inside a library. All the names defined by the standard library are in the std namespace.

One side effect of the library’s use of a namespace is that when we use a name from the library, we must say explicitly that we want to use the name fromthe std namespace. Writing std::cout uses the scope operator (the :: operator) to say that we want to use the name cout that is defined in the namespace std. § 3.1 (p. 82) will show a simpler way to access names from the library.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.5 Introducing Classes (1) 下一篇1.4 Flow of Control (1)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·数据库:推荐几款 Re (2025-12-25 12:17:11)
·如何最简单、通俗地 (2025-12-25 12:17:09)
·什么是Redis?为什么 (2025-12-25 12:17:06)
·对于一个想入坑Linux (2025-12-25 11:49:07)
·Linux 怎么读? (2025-12-25 11:49:04)