开发者

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

开发者 https://www.devze.com 2023-04-13 08:01 出处:网络
Forgive me for possibly asking a fairly simple question, but what does the insertion operator actuall开发者_运维技巧y mean and do in a program? (eg. cout << / cin >>)It depends on how you

Forgive me for possibly asking a fairly simple question, but what does the insertion operator actuall开发者_运维技巧y mean and do in a program? (eg. cout << / cin >>)


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!

    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++ )
            out << v[i] << ' ';
         return v;
      }
    

    And now we can print the vector as:

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

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] << ' ';
      return out;
}

Now you can write this:

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


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.


It writes or reads objects;

std::cout << 5; // writes the integer 5 to the standard output

int x;
std::cin >> x;  // reads an integer from the standard input

It is overloaded for all the standard types.
And most people override them for their own user defined types.

Note: The left hand side of the operator can be any stream type (such as std::fstream or std::stringstream) so it becomes a generalized mechanism for serialization of objects.


<< and >> are simple operators, just as +, -, =, ==, +=, /= etc., you get the drill. That means, it depends on the object / structure you're using it with. With cout and cin these are reading/writing operators, but you could possibly overload the operator to do something completely different.

class myclass {
    int x;
    myclass operator << ( int a ) { 
        x += a;
    }
}

Now, I don't say anyone should do this, but this would result in an addition if you would use a myclass object with this operator. So, as you can see: What you do with "<<" or ">>" depends on how the operator is overloaded.


They are often overloaded and used for streams. << actually is a lef shift operator. >> actually is a right shift operator.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号