[英]std::ostream printing address at end of function
I have the following function: 我有以下功能:
std::vector<double>residuals;
std::cout << Print_res(std::cout);
std::ostream& Print_res(std::ostream& os) const {
os << "\tresidual" << std::endl;
for (unsigned int i = 0 ; i < 22 ; i++) {
os << "\t\t" << residuals[i] << std::endl;
}
os << std::flush;
return os;
};
It prints the residuals correctly, but at the end of the output tags an address as follows: 它可以正确打印残差,但是在输出标记的末尾,地址如下:
2275
2279.08
2224.0835
0x80c5604
how do I fix this? 我该如何解决? EDIT: after reading everyone's comments I replaced the call to the function Print_res
with a std::copy
as 编辑:阅读每个人的评论后,我用std::copy
替换了对函数Print_res
的调用
std::copy(residuals.begin(), residuals.end(), std::ostream_iterator<double>(std::cout,"\n"));
and that did not print the address, so I presume there is something wrong in the way I have written the function. 并且没有打印地址,所以我认为编写函数的方式出了问题。
std::cout << Print_res(std::cout);
This is not legal at global scope so the code that you have posted is not valid. 这在全球范围内是不合法的,因此您发布的代码无效。 If this statement were executed from, say, a function then Print_res
would be called and then the return value of Print_res
would also be streamed to std::cout
. 如果这个说法是从,比如说,一个函数执行然后Print_res
将被称为,然后返回值Print_res
也将被传输到std::cout
。 This is most likely not what you meant. 这很可能不是您的意思。 You probably want just this: 您可能只想要这样:
Print_res(std::cout);
Your statement performs the equivalent of: 您的陈述相当于:
std::cout << std::cout;
In C++03 (which you must be using), std::cout
has an operator void*
(from std::basic_ios<char>
) the result of which is what is being printed. 在C ++ 03(必须使用)中, std::cout
具有operator void*
(来自std::basic_ios<char>
),其结果是所打印的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.