C++ std::ostream函数末尾的打印地址

C++ std::ostream函数末尾的打印地址,c++,C++,我有以下功能: 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 <&

我有以下功能:

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;
};
我该如何解决这个问题? 编辑:在阅读了每个人的评论后,我将对函数
Print\u res
的调用替换为
std::copy
as

 std::copy(residuals.begin(), residuals.end(), std::ostream_iterator<double>(std::cout,"\n"));
std::copy(residuals.begin()、residuals.end()、std::ostream_迭代器(std::cout,“\n”);
这并没有打印地址,所以我认为我编写函数的方式有问题

std::cout << Print_res(std::cout);
您的语句执行的功能相当于:

std::cout << std::cout;
std::cout
您的语句执行的功能相当于:

std::cout << std::cout;

std::cout打印
std::flush
的地址,请发布完整的测试用例<代码>残差
在您的示例中甚至没有声明。@NeelBasu:您能解释为什么它会这样做,而不是导致在
os
上调用
flush
?@NeelBasu:
os
std::endl
已经执行了flush。因此,要么删除显式的
std::flush
,要么使用
'\n'
而不是
std::endl
。这并不能解决您的问题,但无论如何知道这一点是很好的。它正在打印
std::flush的地址,请发布一个完整的测试用例<代码>残差
在您的示例中甚至没有声明。@NeelBasu:您能解释为什么它会这样做,而不是导致在
os
上调用
flush
?@NeelBasu:
os
std::endl
已经执行了flush。因此,要么删除显式的
std::flush
,要么使用
'\n'
而不是
std::endl
。这并不能解决你的问题,但不管怎样,知道这一点是很好的。明白了。这确实是问题所在。谢谢你的解释:-)明白了。这确实是问题所在。非常感谢您的解释:-)
std::cout << std::cout;