Why are bitwise shifts (lt;lt; and gt;gt;) used for cout and cin?(为什么按位移位(lt;lt; 和 gt;gt;)用于 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 ascout=(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.
这篇关于为什么按位移位(<< 和 >>)用于 cout 和 cin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么按位移位(<< 和 >>)用于 cout 和 cin?
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01