问题
I'm wondering if there is a standard name for the "<<" and ">>" operators? This is mostly in the context of teaching C++ and using those operators as part of stream input/output. If I need to read code or prompt for student responses (such as cout << "Hello";
), I'm not sure how to verbalize those symbols. Is there a convention when reading them out loud?
回答1:
When not overloaded, left-shift and right-shift and some people call them that even when used with streams, but insertion and extraction is a lot more common in that context. They are also sometimes informally called put to and get from. IIRC, Stroustrup favoured that last form.
回答2:
According to cplusplus.com's documentation:
This operator (<<) applied to an output stream is known as insertion operator.
...
And from the same website
This operator (>>) applied to an input stream is known as extraction operator.
...
回答3:
Those are officially bitwise shift operators (e.g., 1 << 3
is 8), but they're often overloaded as stream insertion/stream extraction operators (as in the cout example you gave).
回答4:
<<
is the insertion operator. Note when you write
cout << "Some text";
The arrows are pointing to the stream. You're inserting the text into the stream.
>>
is the extraction operator. When you write
cin >> some_var;
You're extracting a value from the stream.
回答5:
In his book "The C++ Programming Language", C++11, bjarne stroustrup has called << "put to" and >> "get from".
Hope this helps
来源:https://stackoverflow.com/questions/42730876/what-is-the-name-of-the-and-operators