stream - Effective use of C++ iomanip library -


I have created a vector class in C ++ and it works great for my problems I am cleaning it now, and I went to the following section of the code:

  std :: ostream & amp; Operator & lt; & Lt; (Std :: ostream & output, const vector & amp; v) {output & lt; & Lt; "[" & Lt; & Lt; Std :: setiosflags (std :: ios :: right | std :: ios :: scientist) & lt; & Lt; Std :: setw (23) to be & lt; & Lt; Std :: setprecision (16) to be & lt; & Lt; V._x & lt; & Lt; "" & Lt; & Lt; Std :: setiosflags (std :: ios :: true | std :: ios :: scientist) & lt; & Lt; Std :: setw (23) to be & lt; & Lt; Std :: setprecision (16) to be & lt; & Lt; V._y & lt; & Lt; "" & Lt; & Lt; Std :: setiosflags (std :: ios :: right | std :: ios :: scientist) & lt; & Lt; Std :: setw (23) & lt; & Lt; Std :: setprecision (16) & lt; & Lt; V _z & lt; & Lt; "]"; Return output; }   

The code will be sent to the vector by std :: cout & lt; & Lt; V & lt; & Lt; Std :: endl; as print. Each number has 23 locations, 16 of which are decimal. The text is correct-aligned, so that it can be printed:

  1.123456123456e + 01 -1.123456123456e + 01   

Instead

< Pre> 1.123456123456e + 01 -1.123456123456e + 01

The code seems very repetitive. How can you "store" formats (all setiosflags , setw and setprecision statements) such as "you can print some letters" In standard form, but numbers with the given format ".

Thank you!

Edit

According to Rob Adams' comment, I changed my ugly code (which, as told by other people, is " The next man will be "accurate") for a more concise (and right):

  std:: ostream & amp; Operator & lt; & Lt; (Std :: ostream & output, const vector & amp; v) {std :: ios_base :: fmtflags f = output.flags (std :: ios :: right | std :: ios :: scientist); Std :: streamsize p = output.precision (16); For production & lt; & Lt; "[" & Lt; & Lt; Std :: setw (23) to be & lt; & Lt; V._x & lt; & Lt; "" & Lt; & Lt; Std :: setw (23) to be & lt; & Lt; V._y & lt; & Lt; "," & Lt; & Lt; Std :: setw (23) & lt; & Lt; V._z & lt; & Lt; "]"; Output.flags (f); Output.precision (p); Return output; }    

only std :: setw () is temporary . The other two calls have the lasting effect of setosphag , and setpersons .

So, you can change your code to:

  std :: ostream & amp; For operator & lt; & Lt; (Std :: ostream & output, constant vector & amp; v) {for production & lt; & Lt; "[" & Lt; & Lt; Std :: setiosflags (std :: ios :: true | std :: ios :: scientist) & lt; & Lt; Std :: setw (23) & lt; & Lt; Std :: setprecision (16) & lt; V._x & lt; & Lt; "," & Lt; & Lt; Std :: setw (23) & lt; & Lt; V._y & lt; & Lt; "," & Lt; & Lt; Std :: setw (23) & lt; & Lt; V._z & lt; & Lt; "]"; Return output; }   

But now you've bored the flags and the exact for the next man instead, try it instead:

  std :: ostream & amp; Operator & lt; & Lt; (Std :: ostream & output, const vector & amp; v) {std :: ios_base :: fmtflags f = output.flags (std :: ios :: right | std :: ios :: scientist); Std :: streamsize p = output.precision (16); For production & lt; & Lt; "[" & Lt; & Lt; Std :: setw (23) to be & lt; & Lt; V._x & lt; & Lt; "" & Lt; & Lt; Std :: setw (23) to be & lt; & Lt; V._y & lt; & Lt; "," & Lt; & Lt; Std :: setw (23) & lt; & Lt; V._z & lt; & Lt; "]"; Output.flags (f); Output.precision (p); Return output; }   

Finally, if you have to get rid of duplication of continuous 23 , then you can do something like this (but I do not recommend it): < / P>

  structure width {int w; Width (int w): w (w) {} buddy std :: ostream & amp; Operator & lt; & Lt; (Std :: ostream & os, const width & amp; w) {return OS & lt; & Lt; Std :: setw (width.w); }}; Std :: ostream & amp; Operator & lt; & Lt; (Std :: ostream & output, const vector & amp; v) {std :: ios_base :: fmtflags f = output.flags (std :: ios :: right | std :: ios :: scientist); Std :: streamsize p = output.precision (16); Width w (23); For production & lt; & Lt; "[" & Lt; & Lt; W & lt; & Lt; V._x & lt; & Lt; "" & Lt; & Lt; W & lt; & Lt; V._y & lt; & Lt; "" & Lt; & Lt; W & LT; & Lt; V._z & lt; & Lt; "]"; Output.flags (f); Output.precision (p); Return output; }   

Also see where they have decided that you can not make the width permanent.

Comments