所以我刚刚开始学习c++,我很好奇它是否是一种用cout格式化输出的方法,所以它看起来很好并且在列中结构化
例如。
string fname = "testname";
string lname = "123";
double height = 1.6;
string fname2 = "short";
string lname2 = "123";
double height2 = 1.8;
cout << "Name" << setw(30) << "Height[m]" << endl;
cout << fname + " " + lname << right << setw(20) << setprecision(2) << fixed << height << endl;
cout << fname2 + " " + lname2 << right << setw(20) << setprecision(2) << fixed << height2 << endl
输出如下所示:
Name Height[m]
testname 123 1.60
short 123 1.80
我希望它看起来像这样:
Name Height[m]
testname 123 1.60
short 123 1.80
我要解决的问题是,我想将高度放置在名称的特定位置,但取决于名称的长度,高度的值要么远离右侧,要么非常靠近左侧。有没有办法来解决这个问题?