为什么我们使用>>和<<在cin和cout中? [英] why we use >> and << in cin and cout?

查看:190
本文介绍了为什么我们使用>>和<<在cin和cout中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在学习c ++
为什么我们使用<<和>>用cin和cout代替括号?

解决方案

这些是重载运算符.在数学或逻辑表达式中,它们是逻辑移位运算符,如此处所示. ="http://msdn.microsoft.com/en-us/library/126fe14k.aspx" target ="_ blank" title ="New Window"> ^ ].但是,在C ++类中,可以重载这些运算符以提供某些其他功能,并且在流中将它们用作插入和提取运算符,如 ^ ]和此处 [^ ] .


除了其他解决方案之外,使用插入和提取运算符也很方便.我猜 Stroustrup [ cout<< " << << endl;


而不是(假设的):

 cout.write(" ).writeln( ).flush(); 



如果您想接受数据到变量中

cin>> a表示接受我们输入到变量"a"中的数据

cout<< a表示您试图显示变量``a''中存在的值

hi i am learning c++
why we use << and >> in cin and cout Instead of parentheses ?

解决方案

These are overloaded operators. In a mathematical or logical expression these are the logical shift operators, as shown here[^]. However in a C++ class these operators can be overloaded to provide some other functionality, and in streams they are used as insertion and extraction operators as described here[^] and here[^].


In addition to the other solutions, using the insertion and extraction operators is just convenience. I guess Stroustrup[^] just found it to be more readable and easier to write:

cout << "The value is: " << value << endl;


instead of (hypothetical):

cout.write("The value is: ").writeln(value).flush();


which could have been an alternative.


if u want to accept data in to a variable

cin>>a means accepting data which we entered into variable ''a''

cout<<a means ur trying to display the value present in variable ''a''


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

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