我想在 C++ 中输出如下:
name aabsd
Size in KB 170
Width and Height 512 512
cout<<"\n \t name "<<std::setw(15)<<filename;
cout<<" \n \t Size in KB "<<std::setw(10)<<size;
cout<< " \n \t Width and Height "<<std::setw(3)<<width<<" "<<height;
右侧的值应在同一列中对齐。我尝试使用 setw() 但它没有给我输出对齐,因为我的左侧文本不同。
另一个依赖于纯 C++ 特定构造的答案。
#include <iostream>
#include <iomanip>
int main()
{
char const* filename = "abcd";
int size = 10;
double width = 20;
double height = 30;
// std::left says align the output to the left when writing the next field
// set::setw(20) says use 20 characters for the next field.
std::cout << std::left << std::setw(20) << "name" << filename << std::endl;
std::cout << std::left << std::setw(20) << "Size in KB" << size << std::endl;
std::cout << std::left << std::setw(20) << "Width and Height" << width << " " << height << std::endl;
return 0;
}