What is the << operator for in C++?

后端 未结 6 1310
抹茶落季
抹茶落季 2021-01-21 13:21

I come from a C# and Java background into C++ and I\'m trying to get to understand the >> & << operators such as in

std         


        
6条回答
  •  独厮守ぢ
    2021-01-21 14:21

    In C and C++ the << operator means "shift left". In C++, when using the standard template library, the << and >> operators are overloaded to implement a streaming API.

    The object std::cout does overload the << operator to use it as a conversion function (in this case to a string printed to the console).

    Actually this:

    int x = myFunction();
    std::cout << x;
    

    and this:

    std::cout << myFunction();
    

    are functionally the same (besides the definition of the temporary variable x).

提交回复
热议问题