Each of the following regards a distinctive ostream
-format. How do I return it to default?
#include <iostream>
int main()
{
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout << "scientific:\n" << std::scientific;
/*Cpp 11 standard only*/
std::cout << " hexfloat: " << std::hexfloat << 0.01 << '\n';
"The number 0.01 in default: " << std::defaultfloat << 0.01; }
}
As in :
std::set_default?;
Also how do I perform
hexfloat and defaultfloat
in Cpp 98 standard?
also what is the technical difference between setwidth and setprecision?
The default of the mutually-exclusive options std::fixed
, std::scientific
, std::hexfloat
and std::defaultfloat
is, you got it std::defaultfloat
.
The default for std::setprecision
is 6
.
The exact effect of std::setw
must be looked up for each stream-inserter separately.
A replacement of std::defaultfloat
for pre-C++11 is easy enough:
std::ios_base& defaultfloat(std::ios_base& str) {
str.unsetf(std::ios_base::floatfield);
return str;
}
Writing std::hexfloat
is equally easy, but the stream-operators wouldn't know what to do with those flags.
Which is non-trivial to fix.
The easy approach to restore the original format is to keep a stream without any modifications around and just use copyfmt()
, eg:
int main() {
std::ostream restore(0);
restore.copyfmt(std::cout);
std::cout.precision(8);
std::cout.copyfmt(restore);
}
This approach will restore all the different formats, including the values stored with pword()
and iword()
. If you want to package this functionality as a set_default
manipulator (you can't put it into namespace std
as only implementers are allowed to put names in there), you'd use something like this:
template <typename cT, typename Traits>
std::basic_ostream<cT, Traits>& set_default(std::basic_ostream<cT, Traits>& out) {
static std::basic_ostream<cT, Traits> dfault(0);
out.copyfmt(dfault);
return out;
}
It would be used like any of the other manipulators, eg:
std::cout << set_default;
You can just have one stream from which to restore the original values. Alternatively, you can keep the format of std::cout
intact and rather create a separate stream using the same buffer but different formats, eg
std::ostream out(std::cout.rdbuf());
out.precision(8);
out << value;
This stream will write to the same stream as std::cout
but use different formatting flags. You can even mix them as the streams don't store any characters directly: this is the job of the shared stream buffer:
std::cout << "value=";
out << value;
std::cout << '\n';
To answer your question about the behavior of, eg, defaultfloat
: these are just manipulator functions. If you want to use them without C++11 you can just define a corresponding function, eg:
template <typename cT, typename Traits>
std::basic_ostream<cT, Traits>& defaultfloat(std::basic_ostream<cT, Traits>& out) {
out.unsetf(std::ios_base::floatfield);
return out;
}
How do I return if to default
auto oldflags = std::cout.flags();
auto oldwidth = std::cout.width();
auto oldprecision = std::cout.precision();
auto oldloc = std::cout.getloc();
auto oldfill = std::cout.fill();
//**************************************************
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout << "scientific:\n" << std::scientific;
/*Cpp 11 standard only*/
std::cout << " hexfloat: " << std::hexfloat << 0.01 << '\n';
"The number 0.01 in default: " << std::defaultfloat << 0.01;
//**********************************************************
std::cout.flags(oldflags);
std::cout.width(oldwidth);
std::cout.precision(oldprecision);
std::cout.imbue(oldloc);
std::cout.fill(oldfill);
It's kind of a pain in the butt.
Also how do I perform hexfloat and defaultfloat in Cpp 98 standard?
Write similar functions yourself. It's even more of a pain in the butt.
what is the technical difference between setwidth and setprecision?
They do completely different things:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.