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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7757278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:26:46  来源:igfitidea点击:

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

c++

提问by user825962

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 >>

回答by Nawaz

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 >> xwould give compilation error.

  • In case of std::cin,>>is used to read from standard input. <<is not overloaded for std::cin. So std::cin << xwould 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

  • 在 的情况下std::cout<<用于写入标准输出。>>没有过载std::cout。所以std::cout >> x会给出编译错误。

  • 在 的情况下std::cin>>用于从标准输入读取。<<没有过载std::cin。所以std::cin << x会给出编译错误。

  • 对于您的自定义类,您可以重载<<>>或两者,并且在该函数中您可以执行任何您喜欢的操作。例如,在下面的代码中,我重载<<forstd::vector<T>以向向量添加元素,

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

    现在我可以使用这个重载来写:

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

    所有整数都添加到向量中!看在线演示:http: //ideone.com/TsqtS

    同样,我们可以重载>>forstd::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;
    }
    

    现在我们可以将向量打印为:

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

    在线演示: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::coutwould look crazy to most programmers, as I guess. A better and probably sane overload would be for std::ostreamas:

关键是你可以以任何你想要的方式重载这些运算符。超载和它们的使用看起来有多疯狂或理智取决于你。例如,v >> std::cout就像我猜的那样,对于大多数程序员来说,语法看起来很疯狂。一个更好的,可能是理智的重载将是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

回答by Cat Plus Plus

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::ostreamor std::istream) and any other type on the right side.

它们是按位移位运算符(<<左移,>>右移)。它们通常也作为流操作符重载(<<然后意味着流输出,>>流输入)——流类型在左侧(例如std::ostreamor std::istream)和任何其他类型在右侧。

回答by Aerius

<< 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. So, as you can see: What you do with "<<" or ">>" depends on how the operator is overloaded.

现在,我不是说任何人都应该这样做,但是如果您将 myclass 对象与此运算符一起使用,则会导致添加。因此,如您所见:您对“<<”或“>>”的处理取决于运算符的重载方式。

回答by Martin York

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),因此它成为对象序列化的通用机制。

回答by Norbert Willhelm

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

它们经常被重载并用于流。<< 实际上是一个左移运算符。>> 实际上是一个右移运算符。