How can I format a number when emitting using yaml-cpp?
I need to output my data in scientific notation with fixed width like the sample below. Is there any way to achieve it?
data:
- [+0.000000e+00, +0.100000e+00, +2.400000e+00, +3.600000e+00, +4.800000e+00] - [+1.开发者_运维百科200000e+00, +1.300000e+00, +2.400000e+00, +4.800000e+00, +6.000000e+00]-SW
Make a wrapper class for your data:
struct Fixed {
Fixed(double v = 0): value(v) {}
double value;
std::string ToString() const {
/* write something that outputs this in the format you want */
}
};
and overload operator <<
:
YAML::Emitter& operator << (YAML::Emitter& out, const Fixed& f) {
out << f.ToString();
return out;
}
Then it'll work as you'd expect:
std::vector<Fixed> data = /* ... */;
YAML::Emitter out;
out << data; // etc
精彩评论