<<和>>在C ++中 [英] << and >> in C++

查看:128
本文介绍了<<和>>在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太明白这意味着什么...我只是从我非常非常非常基本的Python体验学习C ++ ...所以这可能是一个非常愚蠢的问题。我的问题是...说你有你的经典的Hello World程序,你有以下行:

I don't quite understand what this means...I'm just learning C++ from my very very very basic Python experience...and so this may be a very stupid question. My question is...say you have your classic "Hello World" program and you have the line:

cout<<"Hello World!"<<endl;

<意思是...因为我只是看着在C中使用输入,看到你会做一些像:

what does the << mean...because I was just looking at using input in C and saw that you'd do something like:

int i;
cin>>i;

我注意到它有>>而不是<我已经读过那些是按位移位...我并不完全明白这些是什么...但我认为这可能不同这里...帮助...提前感谢

and I noticed that it has >> instead of << and I've read that those are bitwise shifts...and I don't exactly understand what those are...but I think it might be different here...Help...Thanks in advance

推荐答案

在Python中,您可以实现 __ lshift __ __ rshift __ 做任何你想要的。在C ++中,它是相同的 - 而经典的意思是按位右移和按位移左,你可以使它做任何你想要的。

In Python, you can implement __lshift__ and __rshift__ to do whatever you want. In C++, it is the same - while the classic meaning is bitwise shift right and bitwise shift left, you can make it do whatever you want.

这可能是一个在C ++中最明显的违反敏感的运算符重载,但这只是如何 std :: ostream std :: istream 工作。对于所有的C ++爱好者(包括我自己),我为这个奇怪的选择操作符道歉。只是想想它是数据流入的方向( cout 中添加 foo >, cin>> foo cin 放入 从一个新来的人,这真的没有意义,但喝的C ++ Kool Aid,你会 OH YEAH 。相信我。

This is probably one of the most blatant violations of "sensible" operator overloading in C++, but that is just how std::ostream and std::istream work. For all of the C++ lovers out there (myself included), I apologize for this strange choice of operators. Just think of it as the direction that the data flows in (cout << foo puts a foo in cout, cin >> foo puts cin in foo), smile and be happy. From a newcomer, it really doesn't make sense, but drink the C++ Kool Aid and you'll be OH YEAH about it. Trust me.

这篇关于&lt;&lt;和&gt;&gt;在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆