alx*_*mke 5 c++ flags iostream iomanip
我打算将结束使用该resetiosflags函数的行上的所有输出标志重置为默认值。当我尝试以这种方式执行此操作时,它会提供错误的输出,这与我的预期相反。
#include <iostream>
#include <iomanip>
using namespace std;
int
main()
{
bool first;
int second;
long third;
float fourth;
float fifth;
double sixth;
cout << "Enter bool, int, long, float, float, and double values: ";
cin >> first >> second >> third >> fourth >> fifth >> sixth;
cout << endl;
// ***** Solution starts here ****
cout << first << " " << boolalpha << first << endl << resetiosflags;
cout << second << " " << showbase << hex << second << " " << oct << second << endl << resetiosflags;
cout << third << endl;
cout << showpos << setprecision(4) << showpoint << right << fourth << endl << resetiosflags;
cout << scientific << fourth << endl << resetiosflags;
cout << setprecision(7) << left << fifth << endl << resetiosflags;
cout << fixed << setprecision(3) << fifth << endl << resetiosflags;
cout << third << endl;
cout << fixed << setprecision(2) << fourth << endl << resetiosflags;
cout << fixed << setprecision(0) << sixth << endl << resetiosflags;
cout << fixed << setprecision(8) << fourth << endl << resetiosflags;
cout << setprecision(6) << sixth << endl << resetiosflags;
// ***** Solution ends here ****
cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已知的替代方法是通过重述它们来单独取消标记,但这似乎是多余的。
/*unspecified*/ resetiosflags( std::ios_base::fmtflags mask );
Run Code Online (Sandbox Code Playgroud)
std::resetiosflags()是一个操纵器,旨在用于诸如 之类的表达式中out << resetiosfloags( flags )。大概您正在做的是传入一个函数指针,该函数指针通过std::operator<<接受布尔值并打印 1 的重载来选择。
但std::resetiosflags()采用格式标志作为参数,无法操纵精度。std::ios_base::boolalpha但是可以:
std::cout << ... << std::resetiosflags(std::ios_base::boolalpha);
Run Code Online (Sandbox Code Playgroud)
还有std::noboolalpha
std::cout << ... << std::noboolalpha;
Run Code Online (Sandbox Code Playgroud)
但如果您需要将精度重置为默认值,您只需为此创建自己的操纵器即可。您还可以使用Boost IO State Saver。
| 归档时间: |
|
| 查看次数: |
2216 次 |
| 最近记录: |