简体   繁体   English

C ++中cout / cin的“<<”和“>>”是什么意思?

[英]What does “<<” and “>>” mean in C++ for cout/cin?

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 >> ) (例如cout << / cin >>

It depends on how you overload it for you class. 这取决于你如何为你的课超重。

  • In case of std::cout , << is used to write to standard output. std::cout情况下, <<用于写入标准输出。 >> is not overloaded for std::cout . >>没有为std::cout重载。 So std::cout >> x would give compilation error. 所以std::cout >> x会给出编译错误。

  • In case of std::cin , >> is used to read from standard input. std::cin情况下, >>用于从标准输入读取。 << is not overloaded for std::cin . <<没有为std::cin重载。 So std::cin << x would give compilation error. 所以std::cin << x会给出编译错误。

  • 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, 例如,在下面的代码中,我重载<< for std::vector<T>以向向量添加元素,

     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 参见在线演示: http//ideone.com/TsqtS

    Similarly, we can overload >> for std::vector<T> to print all the items in it as: 同样,我们可以重载>> for std::vector<T>来打印它中的所有项目:

     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 在线演示: 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. 例如,对于大多数程序员来说,语法v >> std::cout会让我觉得很疯狂。 A better and probably sane overload would be for std::ostream as: std::ostream更好且可能是理智的重载:

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 演示: http//ideone.com/jce2R

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 (eg std::ostream or std::istream ) and any other type on the right side. 它们也通常作为流操作符重载( <<然后表示流出, >>流入) - 左侧是流类型(例如std::ostreamstd::istream ),右侧是任何其他类型。

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. 注意:运算符的左侧可以是任何流类型(例如std :: fstream或std :: stringstream),因此它成为对象序列化的通用机制。

<< 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. 使用cout和cin这些是读/写操作符,但是你可能会使操作符超载以完成不同的操作。

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. 现在,我没有说任何人都应该这样做,但是如果你在这个运算符中使用myclass对象,这会导致添加。 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. <<实际上是一个lef shift操作员。 >> actually is a right shift operator. >>实际上是一个右移操作员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM