- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我知道有很多次我想重置我使用 iomanip 所做的格式更改。我创建了一个类,您可以使用它来获取 ostream 对象的格式,例如用于写入/读取文件的 std::cout 或 fstream 对象也可以。我这样做主要是为了练习并帮助其他有同样问题的人。
您所要做的就是使用 ResetIOmanip 类创建一个对象。如果您创建时不带参数,它将使用 std::cout 作为默认值。您可以将 ostream/istream 对象作为参数放入,它将对文件起作用。确保在进行任何格式更改之前调用它。然后当它完成时从对象调用成员函数 resetAll() ,它将重置标志、重置精度和重置填充字符。
请随时就如何改进我的头文件提出意见。我仍在学习,并且一直在寻找建设性的批评。谢谢。
最佳答案
这是头文件:
ResetIOmanip.h
/*
File: ResetIOmanip.h
Author: kingcobra1986
Date: 7/11/2016
Class: ResetIOmanip
Purpose:
When the instance of the class ResetIOmanip is created, the
current settings will be saved and can be recalled by calling
the member function resetAll().
*/
#ifndef _RESETIOMANIP
#define _RESETIOMANIP
#include <iostream>
#include <iomanip>
class ResetIOmanip {
private:
std::ostream * _ostream;
std::istream * _istream;
std::streamsize _precision;
std::ios_base::fmtflags _flags;
char _fill;
//Set the original precision
void set_precision () {
if (this->_ostream != nullptr)
this->_precision = this->_ostream->precision ();
if (this->_istream != nullptr)
this->_precision = this->_istream->precision ();
}
//Set the original flags
void set_flags () {
if (this->_ostream != nullptr)
this->_flags = this->_ostream->flags ();
if (this->_istream != nullptr)
this->_flags = this->_istream->flags ();
}
//Set the original fill
void set_fill () {
if (this->_ostream != nullptr)
this->_fill = this->_ostream->fill ();
if (this->_istream != nullptr)
this->_fill = this->_istream->fill ();
}
public:
//Default Constructor
ResetIOmanip ( std::ostream & stream = std::cout ) {
this->_ostream = &stream;
this->_istream = nullptr;
this->set_precision ();
this->set_flags ();
this->set_fill ();
stream << "Fill: " << this->get_fill () << std::endl;
}
//Overloaded Constructor
ResetIOmanip ( std::istream & stream ) {
this->_ostream = nullptr;
this->_istream = &stream;
this->set_precision ();
this->set_flags ();
this->set_fill ();
}
//Get the original precision
std::streamsize get_precision () { return this->_precision; }
//Get the original flags
std::ios_base::fmtflags get_flags () { return this->_flags; }
//Get the original fill
char get_fill () { return this->_fill; }
//Reset to the original precision
void reset_precision () {
if (this->_ostream != nullptr)
this->_ostream->precision ( this->_precision );
if (this->_istream != nullptr)
this->_istream->precision ( this->_precision );
}
//Reset to the original flags
void reset_flags () {
if (this->_ostream != nullptr)
this->_ostream->flags ( this->_flags );
if (this->_istream != nullptr)
this->_istream->flags ( this->_flags );
}
//Reset to the original fill
void reset_fill () {
if (this->_ostream != nullptr) {
this->_ostream->fill ( this->_fill );
}
if (this->_istream != nullptr)
this->_istream->fill ( this->_fill );
}
//Reset to all of the original settings
void resetAll () {
this->reset_precision ();
this->reset_flags ();
this->reset_fill ();
}
};
#endif
我是这样测试的:
main.cpp
//This is to test ResetIOmanip objects
#include <iostream>
#include <fstream>
#include "ResetIOmanip.h"
int main () {
using std::cout;
using std::endl;
double randNumb1 = 45.235723;
double randNumb2 = 1.49;
ResetIOmanip resetFormats1;
cout << "Original 1: " << randNumb1 << endl;
cout << "Original 2: " << randNumb2 << endl;
cout << "setprecision(4) for #1: " << std::setprecision ( 4 ) << randNumb1 << endl;
cout << "setprecision(4) for #2: " << std::setprecision ( 4 ) << randNumb2 << endl << endl;
cout << "setprecision(4) and fixed for #1: " << std::fixed << std::setprecision ( 4 ) << randNumb1 << endl;
cout << "setprecision(4) and fixed for #2: " << std::fixed << std::setprecision ( 4 ) << randNumb2 << endl;
cout << "setfill(x) and setw(10) for #1: " << std::setfill ( 'x' ) << std::setw ( 10 ) << randNumb1 << endl;
cout << "setfill(x) and setw(10) for #2: " << std::setfill ( 'x' ) << std::setw ( 10 ) << randNumb2 << endl << endl;
cout << "Testing Format #1: " << randNumb1 << endl;
cout << "Testing Format #2: " << randNumb2 << endl;
cout << "Testing Format with set width(10) #1: " << std::setw(10) << randNumb1 << endl;
cout << "Testing Format with set width(10) #2: " << std::setw ( 10 ) << randNumb2 << endl << endl;
cout << "RESETING - TESTING CLASS" << endl;
resetFormats1.resetAll ();
cout << "Testing Format #1: " << randNumb1 << endl;
cout << "Testing Format #2: " << randNumb2 << endl;
cout << "Testing Format with set width(10) #1: " << std::setw ( 10 ) << randNumb1 << endl;
cout << "Testing Format with set width(10) #2: " << std::setw ( 10 ) << randNumb2 << endl << endl;
cout << "Testing with ostream to testResetIOmanip.txt" << endl;
std::ofstream testingOut;
testingOut.open ( "testResetIOmanip.txt", std::ios::out );
if (testingOut.fail ()) {
cout << "ERROR: Cannot open the file" << endl;
return 0;
}
ResetIOmanip resetFormats2 ( testingOut );
testingOut << "Original 1: " << randNumb1 << endl;
testingOut << "Original 2: " << randNumb2 << endl;
testingOut << "setprecision(4) for #1: " << std::setprecision ( 4 ) << randNumb1 << endl;
testingOut << "setprecision(4) for #2: " << std::setprecision ( 4 ) << randNumb2 << endl << endl;
testingOut << "setprecision(4) and fixed for #1: " << std::fixed << std::setprecision ( 4 ) << randNumb1 << endl;
testingOut << "setprecision(4) and fixed for #2: " << std::fixed << std::setprecision ( 4 ) << randNumb2 << endl;
testingOut << "setfill(x) and setw(10) for #1: " << std::setfill ( 'x' ) << std::setw ( 10 ) << randNumb1 << endl;
testingOut << "setfill(x) and setw(10) for #2: " << std::setfill ( 'x' ) << std::setw ( 10 ) << randNumb2 << endl << endl;
testingOut << "Testing Format #1: " << randNumb1 << endl;
testingOut << "Testing Format #2: " << randNumb2 << endl;
testingOut << "Testing Format with set width(10) #1: " << std::setw ( 10 ) << randNumb1 << endl;
testingOut << "Testing Format with set width(10) #2: " << std::setw ( 10 ) << randNumb2 << endl << endl;
testingOut << "RESETING - TESTING CLASS" << endl;
resetFormats2.resetAll ();
testingOut << "Testing Format #1: " << randNumb1 << endl;
testingOut << "Testing Format #2: " << randNumb2 << endl;
testingOut << "Testing Format with set width(10) #1: " << std::setw ( 10 ) << randNumb1 << endl;
testingOut << "Testing Format with set width(10) #2: " << std::setw ( 10 ) << randNumb2 << endl << endl;
testingOut.close ();
return 0;
}
关于c++ - 在一次调用中重置使用 iomanip : setprecision, setfill 和标志所做的所有更改的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38323316/
在 C++ 中,例如,当您编写 setprecision(12) 时,12 是以 10 为基数的,但是当您将其编写为 setprecision(012) 时,它是一个八进制数,为什么? 最佳答案 因为
我有以下功能片段: bool A_AbstractTester_Actor::Equals( int lineNumber, T_String valueText, double value, T_S
嗨,虽然我做了这样的事情 #include ... std::cout<<"Numbers "<
这是我的大部分代码,我需要将最后一个 cout 的精度设置为 2,这样我就可以得到小数点后的位置: #include #include #include #include using name
我正在做一个程序,首先从用户那里获取 2 个数字(具有 float 数据类型),然后询问用户他想要将数字除以什么数字,最后将它除以那个数字和' cout #include #include usi
我认为 setprecision 不会改变变量本身的值。此外,当您将 setprecision 附加到 cout 时,它只会坚持一次。但是,当我运行代码进行验证时,它不起作用。 考虑以下代码片段: i
以下代码: double x = 3.14; double y = 3.14159265359; cout<
我正在学习 C++,但我不断收到一个奇怪的错误。 setprecision 在一个答案中给了我多个小数点。 为什么输出有多个小数点? 程序 #include #include #include usi
这个问题在这里已经有了答案: Correct use of std::cout.precision() - not printing trailing zeros (3 个答案) 关闭 6 年前。
我似乎遇到了 C++ 编码问题。它涉及数学运算,我似乎让我的所有输出都正确,除了最后一个。除此之外,我的答案的小数点格式似乎不正确。答案应该包含两位小数,但我的四个小数点答案中只有两个似乎有两位小数。
我有一个文本文件,其中每一行都包含一些值,例如: xxx.xxxxxx 我正在读取每一行并使用 strod() 将它们转换为 str 并将它们存储在一个临时 double 变量中。 当我打印那个 do
堆栈溢出和 C++ 新手 std::cout << std::fixed << "Starting Balance: $" << std::setprecision(2) << startbal <<
我正在构建一个计算器应用程序,用于计算准确的税后净工资。我遇到的问题是程序会自动将值四舍五入。 我试过使用 setprecision,但它似乎只在 cout 或 cin 行中使用时才有效。这将需要用户
double value = 02369.000133699;//acutally stored as 2369.000133698999900 const std::uint32_t left =
据我所知,setprecision 函数指定了最小精度,但是当我运行以下代码时,我只得到小数点后的 3 个数字: int main() { double a = 123.4567890;
当流到标准输出时,我可以指定 setprecision 来舍入 double 值吗? ofile , 设置舍入方向: #include #include #include #include
当我编译下面的代码时,编译器给我警告: "Implicit conversion loses integer precision: 'std::streamsize' (aka 'long') to
当我编译下面的代码时,编译器给我警告: "Implicit conversion loses integer precision: 'std::streamsize' (aka 'long') to
我正在尝试以格式化文本进行一些简单的输出。 Setprecision 没有将我的变量打印到小数点后两位。 例如,如果 firstItemPrice = 2.20,则输出为 2.2 而不是 2.20 #
为什么有 BigDecimal.setScale() 方法将小数设置为某个值,而没有 MathContext.setScale() 方法,只有构造函数 MathContext (精度) 或其他方式解决
我是一名优秀的程序员,十分优秀!