C ++如何正确证明多条数据的合理性 [英] C++ how to right justify multiple pieces of data
问题描述
所以我必须向std::cout
发送一列数据,在其中我还必须在数据周围显示一些字符,例如:
So I have to send to std::cout
a column of data where I have to also display some characters around the data like:
Plot Points
(0,0), (2,3)
(1,10), (12,14)
在这里我必须右对齐列标题中"Points"
中字母"s"
下的最后一个右括号.
where I have to right justify the last right parenthesis under the letter "s"
in "Points"
in the column header.
我正在像这样发布数据
cout << right << setw(12) << "(" << x1 << "," << y1 << "), (" << x2 << "," << y2 << ")";
但是我看到的所有示例似乎都表明正确的setw
似乎只影响我发送给cout
的下一条数据,因此在这种情况下仅影响"("
.
But all the examples I have seen seem to show that the right and setw
only seem to affect the next piece of data I send to cout
, so in this case the "("
only.
有没有办法将所有这些字符和变量组合在一起,以使它们在输出列中全部对齐?
Is there any way to group all those characters and variables together to have them all justified together in the column of output?
我只是在学习C ++,所以希望有一些我还没有学到的简单解决方案吗?
I'm just learning C++ so expect there is some simple solution I haven't learned yet?
推荐答案
有没有办法将所有这些字符和变量组合在一起,以使它们在输出列中全部对齐?
Is there any way to group all those characters and variables together to have them all justified together in the column of output?
是的,您可以使用一些辅助函数来构建字符串:
Yes, you can use a little helper function to build a string:
std::string parentized_pair(int x, int y) {
std::ostringstream oss;
oss << "(" << x "," << y << ")";
return oss.str();
}
并在最终输出中使用那个:
and use that one in the final output:
cout << right << setw(12) << parentized_pair(x1,y1)
<< right << setw(12) << parentized_pair(x2,y2);
这篇关于C ++如何正确证明多条数据的合理性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!