关于C++标准输出的有关问题
关于C++标准输出的问题?
#include <iostream>
using namespace std;
int main()
{
std::cout < < "Enter two numbers: " < <std::endl;
int v1,v2;
std::cin> > v1> > v2;
(std::cout < < "the sum of: ") < <v1 < <(std::cout < < "and " < <v2) < < "is " < <v1+v2 < <std::endl;
return 0;
}
运行上述程序,如果输入2 3 回车后输出结果是什么啊?
我的计算机输出是:and3the sum of:10047FE8Cis5
问什么会这样呢?10047FE8C是什么?
------解决方案--------------------
(std::cout < < "the sum of: ") < <v1 < <(std::cout < < "and " < <v2) < < "is " < <v1+v2 < <std::endl;
为什么不把那些括号都去掉啊。
中间怎么把一个std::cout对象给输出了?
------解决方案--------------------
这里的问题主要是operator < <的细节,当你用 < <时,实际上是在调用operator < <,在c++标准中,operator < <返回一个ostream引用(这里即为cout引用),正是因为 < <才可以串联使用。
上述程序中,第一个括号可以省略,因为本来就是首先执行的,执行的结果是将the sum of:插入输出流并返回cout的引用,此引用再将v1插入到输出流,这里关键的一点是the sum of :3并没有被输出到控制台,而是进入了缓冲区。然后执行第二个括号中的语句,执行结果是输出了and3并返回cout的引用。其实上述程序中的输出语句相当于以下两句:
cout < < "and " < <v2;
cout < < "the sum of: " < <v1 < <&cout < < "is " < <v1+v2 < <endl;
其中的&cout是cout对象的起始地址,在楼主的运行过程中即为10047FE8C
------------
等待高手的出现。。。
------解决方案--------------------
假如输入2 3
相当于:
void* pvtemp=(cout < < "and " < <v2); //对表达式求值
(cout < < "the sum of: ") < <v1 < <pvtemp < < "is " < <v1+v2 < <endl;
表达式(cout < < "and " < <v2)的运算:对((cout < < "and ") < <v2)返回的对象调用operator void *,也就是返回cout对象的地址。
显示and3
显示and3the sum of:2 //表达式cout < < "the sum of: ";返回cout的引用,继续cout < <v1;
显示and3the sum of:20045766C //继续cout < <pvtemp;
显示and3the sum of:20045766Cis5 //cout < < "is " < <v1+v2 < <endl;
两个括号的效果不同是因为cout的引用是不能作为operator < <的第二个参数放在其右边的,只能作为其第一个参数放在左边
#include <iostream>
using namespace std;
int main()
{
std::cout < < "Enter two numbers: " < <std::endl;
int v1,v2;
std::cin> > v1> > v2;
(std::cout < < "the sum of: ") < <v1 < <(std::cout < < "and " < <v2) < < "is " < <v1+v2 < <std::endl;
return 0;
}
运行上述程序,如果输入2 3 回车后输出结果是什么啊?
我的计算机输出是:and3the sum of:10047FE8Cis5
问什么会这样呢?10047FE8C是什么?
------解决方案--------------------
(std::cout < < "the sum of: ") < <v1 < <(std::cout < < "and " < <v2) < < "is " < <v1+v2 < <std::endl;
为什么不把那些括号都去掉啊。
中间怎么把一个std::cout对象给输出了?
------解决方案--------------------
这里的问题主要是operator < <的细节,当你用 < <时,实际上是在调用operator < <,在c++标准中,operator < <返回一个ostream引用(这里即为cout引用),正是因为 < <才可以串联使用。
上述程序中,第一个括号可以省略,因为本来就是首先执行的,执行的结果是将the sum of:插入输出流并返回cout的引用,此引用再将v1插入到输出流,这里关键的一点是the sum of :3并没有被输出到控制台,而是进入了缓冲区。然后执行第二个括号中的语句,执行结果是输出了and3并返回cout的引用。其实上述程序中的输出语句相当于以下两句:
cout < < "and " < <v2;
cout < < "the sum of: " < <v1 < <&cout < < "is " < <v1+v2 < <endl;
其中的&cout是cout对象的起始地址,在楼主的运行过程中即为10047FE8C
------------
等待高手的出现。。。
------解决方案--------------------
假如输入2 3
相当于:
void* pvtemp=(cout < < "and " < <v2); //对表达式求值
(cout < < "the sum of: ") < <v1 < <pvtemp < < "is " < <v1+v2 < <endl;
表达式(cout < < "and " < <v2)的运算:对((cout < < "and ") < <v2)返回的对象调用operator void *,也就是返回cout对象的地址。
显示and3
显示and3the sum of:2 //表达式cout < < "the sum of: ";返回cout的引用,继续cout < <v1;
显示and3the sum of:20045766C //继续cout < <pvtemp;
显示and3the sum of:20045766Cis5 //cout < < "is " < <v1+v2 < <endl;
两个括号的效果不同是因为cout的引用是不能作为operator < <的第二个参数放在其右边的,只能作为其第一个参数放在左边