Why are bitwise shifts (<< and>>) used for cout and cin?

后端 未结 13 1680
眼角桃花
眼角桃花 2020-12-02 16:16

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

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 16:52

    Insertion operator >> and << are used with Input Stream and Output Stream respectively because Input stream means flow of data into your program and Output stream means flow of data out of your program. As these insertion operators look like Directional operator (Showing direction of flow of data), so >> is chosen for Input stream and << for the Output stream.

    Have a look at the part of code...

    int Num1;
    cin >> Num1;
    

    here if you observe carefully >> is showing flow of data to variable (declared in program) that means the flow of data to the program , which is a job of Input stream (here cin).

    similarly goes with cout,

    int Num2 = 5;
    cout << Num2;
    

    Here << showing the flow of data out of the program (as Num2 is part of the program), which is the job of Output stream.

    I hope all this make sense to you.

提交回复
热议问题