Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "<<" and ">>" mean in C++ for cout/cin?

Tags:

c++

Forgive me for possibly asking a fairly simple question, but what does the insertion operator actually mean and do in a program? (eg. cout << / cin >>)

like image 555
user825962 Avatar asked Oct 13 '11 16:10

user825962


People also ask

What does << mean in cout?

In this case, the subject " cout " means "the standard character output device", and the verb " << " means "output the object" — in other words, the command " cout << " means "send to the standard output stream," (in this case we assume the default, the console).

Why << is used with cout?

It is used to display the output to the standard output device i.e. monitor. It is associated with the standard C output stream stdout. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).

What is the use of << and >> in C++?

The bitwise shift operators are the right-shift operator (>>), which moves the bits of shift_expression to the right, and the left-shift operator (<<), which moves the bits of shift_expression to the left.

What does Cin << mean in C++?

Conclusion. The "c" in C++ cin refers to "character" and "in" means "input". Thus, cin means "character input". The C++ cin object belongs to the istream class. It accepts input from a standard input device, such as a keyboard, and is linked to stdin, the regular C input source.


2 Answers

It depends on how you overload it for you class.

  • In case of std::cout, << is used to write to standard output. >> is not overloaded for std::cout. So std::cout >> x would give compilation error.

  • In case of std::cin,>> is used to read from standard input. << is not overloaded for std::cin. So std::cin << x would give compilation error.

  • For your custom class, you can overload << or >>, or both, and in the function you can do anything you like. For example, in the following code, I overload << for std::vector<T> to add elements to vector,

    template<typename T> std::vector<T> & operator<<(std::vector<T> & v, T const & item) {       v.push_back(item);       return v; } 

    Now I can use this overload to write this:

    std::vector<int> v; v << 1 << 2 << 3 << 4 << 5; //inserts all integers to the vector! 

    All the integers are added to the vector! See online demo : http://ideone.com/TsqtS

    Similarly, we can overload >> for std::vector<T> to print all the items in it as:

    template<typename T> std::vector<T> & operator>>(std::vector<T> & v, std::ostream & out) {    for(size_t i = 0 ; i < v.size(); i++ )       std::cout << v[i] << std::endl;    return v; } 

    And now we can print the vector as:

    v >> std::cout; //crazy! 

    Online demo : http://ideone.com/BVSm7

The point is that you can overload these operators in whatever way you want. How crazy or sane the overload and their usage would look is up to you. For example, the syntax v >> std::cout would look crazy to most programmers, as I guess. A better and probably sane overload would be for std::ostream as:

template<typename T> std::ostream & operator << (std::ostream & out, const std::vector<T> & v) {       for(size_t i = 0 ; i < v.size(); i++ )          out << v[i] << std::endl;       return out; } 

Now you can write this:

std::cout << v << std::endl; //looks sane! 

Demo : http://ideone.com/jce2R

like image 144
Nawaz Avatar answered Sep 21 '22 17:09

Nawaz


They're bitwise shift operators (<< is shift left, >> is shift right). They're also commonly overloaded as streaming operators (<< then means stream out, >> stream in) — with stream type on the left side (e.g. std::ostream or std::istream) and any other type on the right side.

like image 22
Cat Plus Plus Avatar answered Sep 19 '22 17:09

Cat Plus Plus