1

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

4

1 回答 1

3

为您的数据创建一个包装类:

struct Fixed {
   Fixed(double v = 0): value(v) {}
   double value;

   std::string ToString() const {
      /* write something that outputs this in the format you want */
   }
};

和过载operator <<

YAML::Emitter& operator << (YAML::Emitter& out, const Fixed& f) {
   out << f.ToString();
   return out;
}

然后它会像你期望的那样工作:

std::vector<Fixed> data = /* ... */;
YAML::Emitter out;
out << data;  // etc
于 2011-06-25T00:17:39.983 回答