文章目录
1. setiosflags函数的相关问题
1.1 两种解答
1.1.1 正确解答
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
int N;
string name, code;
double cost;
cin >> N;
while(N--) {
cin >> name >> code >> cost;
cout << setw(15) << left << name <<
setw(15) << left << code <<
setw(10) << right << setprecision(2) << fixed << cost << endl;
}
return 0;
}
1.1.2 错误解答
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
int N;
string name, code;
double cost;
cin >> N;
while(N--) {
cin >> name >> code >> cost;
cout << setw(15) << setiosflags(ios::left) << name <<
setw(15) << setiosflags(ios::left) << code <<
setw(10) << setiosflags(ios::right) << setprecision(2) << fixed << cost << endl;
}
}
1.2 原因分析
1.2.1 主要问题
- 两种解答的主要区别:输出的格式从第二行开始不相同
- 错误解答的输出格式与所想实现的不同
1.2.2 原因
通过查看源码,可以得知这两种设置方式所调用的具体函数是不同的
cout << setiosflags(ios::left);
// 等价于
setf(ios::left);
// 调用函数
fmtflags setf( fmtflags flags );
cout << left;
// 等价于
setf(ios::left, ios::adjustfield);
// 调用函数
fmtflags setf( fmtflags flags, fmtflags mask );
这两个函数的实际说明和实现可参考给出的链接和源码。
1.2.3 实际分析
- 正确的解答:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
int N;
string name, code;
double cost;
cin >> N;
while(N--) {
cin >> name >> code >> cost;
cout << setw(15) << left << name << endl;
cout << cout.flags() << endl;
cout << setw(15) << left << code << endl;
cout << cout.flags() << endl;
cout << setw(10) << right << setprecision(2) << fixed << cost << endl;
cout << cout.flags() << endl;
}
return 0;
}
// output
// 4130 4130 4230 4134 4134 4230 4134 4134 4230
- 错误的解答:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
int N;
string name, code;
double cost;
cin >> N;
while(N--) {
cin >> name >> code >> cost;
cout << setw(15) << setiosflags(ios::left) << name << endl;
cout << cout.flags() << endl;
cout << setw(15) << setiosflags(ios::left) << code << endl;
cout << cout.flags() << endl;
cout << setw(10) << setiosflags(ios::right) << setprecision(2) << fixed << cost << endl;
cout << cout.flags() << endl;
}
}
// output
// 4130 4130 4262 4262 4262 4262 4262 4262 4262
-
left
、right
等fmtflags
类型变量值如图所示:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6nsp1Es2-1678587225356)(./1.png)] -
通过观察实际的输出,可以验证这两个函数对
fl
的操作与参考链接中描述的相同fmtflags setf( fmtflags flags );
中fl = fl | flags
fmtflags setf( fmtflags flags, fmtflags mask );
中fl = (fl & ~mask) | (flags & mask)
-
此外,
fl
的left
等设置会按照一定的条件进行输出,具体条件如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RPNGCO5X-1678587225357)(./2.png)] -
所以,在错误的解答中
left
和right
同时被设置,这时就会按照right
的格式进行输出;如果同时设置left
和internal
,也会按照right
的格式进行输出
1.2.4 解决办法
- 如果想使用
setiosflags
函数设置输出格式,需要使用resetiosflags
函数清除之前的设置 - 修改后可实现想要的输出格式
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
int N;
string name, code;
double cost;
cin >> N;
while(N--) {
cin >> name >> code >> cost;
cout << setw(15) << resetiosflags(ios::right) << setiosflags(ios::left) << name <<
setw(15) << resetiosflags(ios::left) << setiosflags(ios::left) << code <<
setw(10) << resetiosflags(ios::left) << setiosflags(ios::right) << setprecision(2) << fixed << cost << endl;
}
}
参考链接
- https://blog.csdn.net/qwe641259875/article/details/92619610
- https://en.cppreference.com/w/cpp/io/manip/left
- https://en.cppreference.com/w/cpp/io/ios_base/flags
- https://en.cppreference.com/w/cpp/io/ios_base/setf
- https://en.cppreference.com/w/cpp/io/manip/setiosflags
- https://en.cppreference.com/w/cpp/io/manip/resetiosflags
- https://en.cppreference.com/w/cpp/io/ios_base/fmtflags
- https://en.cppreference.com/w/cpp/named_req/FormattedOutputFunction