为什么按位移位(<< 和 >>)用于 cout 和 cin?

Why are bitwise shifts (lt;lt; and gt;gt;) used for cout and cin?(为什么按位移位(lt;lt; 和 gt;gt;)用于 cout 和 cin?)

本文介绍了为什么按位移位(<< 和 >>)用于 cout 和 cin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题确实在标题中;我敢肯定有一些合乎逻辑的,但现在我很难过!

Question is in the title really; I'm sure there is something logical, but for now I'm stumped!

推荐答案

根据C++的设计与演化的§8.3.1:

Doug McIlroy 通过类比 UNIX shell 中的 I/O 重定向操作符(>、>>、| 等)提出了提供输出操作符而不是命名输出函数的想法

The idea of providing an output operator rather than a named output function was suggested by Doug McIlroy by analogy with the I/O redirection operators in the UNIX shell (>, >>, |, etc.)

[...]

输入和输出操作考虑了几个操作符:赋值运算符是输入和输出的候选者,但它以错误的方式绑定.即 cout=a=b 将被解释为 cout=(a=b),并且大多数人似乎更喜欢输入运算符与输出运算符不同.运算符<>都试过了,但是小于"和大于"的含义已经牢牢地植入人们的脑海中,以至于新的I/O语句从所有实际目的来看都是不可读的(<<<>>> 似乎不是这种情况).除此之外,'<'就在大多数键盘上的,"上方,人们写这样的表达方式:

Several operators were considered for input and output operations: the assignment operator was a candidate for both input and output, but it binds the wrong way. That is cout=a=b would be interpreted as cout=(a=b), and most people seemed to prefer the input operator to be different from the output operator. The operators < and > were tried, but the meanings "less than" and "greater than" were so firmly implanted in people's minds that the new I/O statements were for all practical purposes unreadable (this does not appear to be the case for << and >>). Apart from that, '<' is just above ',' on most keyboards, and people were writing expressions like this:

cout < x , y, z;

为此提供好的错误消息并不容易.

It is not easy to give good error messages for this.

这篇关于为什么按位移位(&lt;&lt; 和 &gt;&gt;)用于 cout 和 cin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么按位移位(&lt;&lt; 和 &gt;&gt;)用于 cout 和 cin?

基础教程推荐