cpp中setiosflags(ios::left)和std::left设置输出格式的区别

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
  • leftrightfmtflags类型变量值如图所示:
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6nsp1Es2-1678587225356)(./1.png)]

  • 通过观察实际的输出,可以验证这两个函数对fl的操作与参考链接中描述的相同

    • fmtflags setf( fmtflags flags );fl = fl | flags
    • fmtflags setf( fmtflags flags, fmtflags mask );fl = (fl & ~mask) | (flags & mask)
  • 此外,flleft等设置会按照一定的条件进行输出,具体条件如下:
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RPNGCO5X-1678587225357)(./2.png)]

  • 所以,在错误的解答中leftright同时被设置,这时就会按照right的格式进行输出;如果同时设置leftinternal,也会按照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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值