C++ 的CIN和COUT操作符

来源:互联网 发布:做柱状图的软件 编辑:程序博客网 时间:2024/05/10 09:21

C++中的CIN和COUT是两个操作符(operator),且是左操作符,相同于+, -等操作符,而且与这些操作符一样,返回的表达式值为左值。看下面这个例子:

#include <iostream>

int main(int argc, char **argv)

{

std::cout << "Enter two numbers: " << std::endl;

int v1, v2;

std::cin >> v1 >> v2;

std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2

<< std::endl;

return 0;

}

在第7行CIN的例子中,前面的std::cin >>v1表达式表示从cin中读入一个值,赋给v1,表达式左操作数(left-handoperand)为std::cin,右操作数为(right-handoperand),表达式返回左操作值,即std::cin,于是第7行相当于下面两行:

std::cin >> v1;

std::cin >> v2;

与CIN类似,COUT也是左操作符,第9到10行的解释与上面类似

原创粉丝点击