1. <small id='jvM48'></small><noframes id='jvM48'>

      <legend id='jvM48'><style id='jvM48'><dir id='jvM48'><q id='jvM48'></q></dir></style></legend>
        <bdo id='jvM48'></bdo><ul id='jvM48'></ul>

      1. <i id='jvM48'><tr id='jvM48'><dt id='jvM48'><q id='jvM48'><span id='jvM48'><b id='jvM48'><form id='jvM48'><ins id='jvM48'></ins><ul id='jvM48'></ul><sub id='jvM48'></sub></form><legend id='jvM48'></legend><bdo id='jvM48'><pre id='jvM48'><center id='jvM48'></center></pre></bdo></b><th id='jvM48'></th></span></q></dt></tr></i><div id='jvM48'><tfoot id='jvM48'></tfoot><dl id='jvM48'><fieldset id='jvM48'></fieldset></dl></div>
        <tfoot id='jvM48'></tfoot>

      2. C++ iomanip库的有效使用

        时间:2024-08-14
          <tbody id='wW5mA'></tbody>
        • <legend id='wW5mA'><style id='wW5mA'><dir id='wW5mA'><q id='wW5mA'></q></dir></style></legend>

          <i id='wW5mA'><tr id='wW5mA'><dt id='wW5mA'><q id='wW5mA'><span id='wW5mA'><b id='wW5mA'><form id='wW5mA'><ins id='wW5mA'></ins><ul id='wW5mA'></ul><sub id='wW5mA'></sub></form><legend id='wW5mA'></legend><bdo id='wW5mA'><pre id='wW5mA'><center id='wW5mA'></center></pre></bdo></b><th id='wW5mA'></th></span></q></dt></tr></i><div id='wW5mA'><tfoot id='wW5mA'></tfoot><dl id='wW5mA'><fieldset id='wW5mA'></fieldset></dl></div>
            <bdo id='wW5mA'></bdo><ul id='wW5mA'></ul>
                <tfoot id='wW5mA'></tfoot>

                <small id='wW5mA'></small><noframes id='wW5mA'>

                1. 本文介绍了C++ iomanip库的有效使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我用 C++ 创建了一个 Vector 类,它非常适合我的问题.我现在正在清理它,我遇到了以下代码:

                  I created a Vector class in C++ and it works great for my problems. I am now cleaning it up, and I ran into the following piece of code:

                  std::ostream& operator<<(std::ostream &output, const Vector &v){
                    output<<"["
                      <<std::setiosflags(std::ios::right | std::ios::scientific)
                      <<std::setw(23)
                      <<std::setprecision(16)
                      <<v._x<<", "
                      <<std::setiosflags(std::ios::right | std::ios::scientific)
                      <<std::setw(23)
                      <<std::setprecision(16)
                      <<v._y<<", "
                      <<std::setiosflags(std::ios::right | std::ios::scientific)
                      <<std::setw(23)
                      <<std::setprecision(16)
                      <<v._z<<"]";
                    return output;
                  } 
                  

                  该代码允许将向量打印为 std::cout<<v<<std::endl;.每个数字有23个空格,其中16个是小数.文本右对齐以便打印:

                  The code allows to print a vector as std::cout<<v<<std::endl;. Each number has 23 spaces, of which 16 are the decimals. The text is right-aligned so that it will print:

                   1.123456123456e+01
                  -1.123456123456e+01
                  

                  代替

                  1.123456123456e+01
                  -1.123456123456e+01
                  

                  代码看起来非常重复.您如何存储"格式(所有 setiosflagssetwsetprecision 语句),以便您可以说打印字符以标准方式,但具有这种给定格式的数字".

                  The code seems awfully repetitive. How can you "store" the format (all the setiosflags, setw and setprecision statements) such that you can say something like "print the characters in a standard way, but the numbers with this given format".

                  谢谢!

                  编辑

                  根据 Rob Adams 的评论,我将丑陋的代码(正如其他人指出的那样,会破坏下一个人"的精确度)更改为更简洁(且正确):

                  As per Rob Adams' comment, I changed my ugly code (which, as pointed out by others, would mess up the precision for the "next guy") to a more succinct (and correct):

                  std::ostream& operator<<(std::ostream &output, const Vector &v){
                    std::ios_base::fmtflags f = output.flags(std::ios::right | std::ios::scientific);
                    std::streamsize p = output.precision(16);
                    output<<"["
                      <<std::setw(23)<<v._x<<", "
                      <<std::setw(23)<<v._y<<", "
                      <<std::setw(23)<<v._z
                      <<"]";
                    output.flags(f);
                    output.precision(p);
                    return output;
                  }
                  

                  推荐答案

                  只有 std::setw() 是临时的.另外两个调用 setiosflagssetprecision 具有永久效果.

                  Only std::setw() is temporary. The other two calls, setiosflags, and setprecision have a permanent effect.

                  因此,您可以将代码更改为:

                  So, you could change your code to :

                  std::ostream& operator<<(std::ostream &output, const Vector &v){
                    output<<"["
                      <<std::setiosflags(std::ios::right | std::ios::scientific)
                      <<std::setw(23)
                      <<std::setprecision(16)
                      <<v._x<<", "
                      <<std::setw(23)
                      <<v._y<<", "
                      <<std::setw(23)
                      <<v._z<<"]";
                    return output;
                  } 
                  

                  但现在你已经为下一个家伙浪费了旗帜和精确度.试试这个:

                  But now you've borked the flags and precision for the next guy. Try this instead:

                  std::ostream& operator<<(std::ostream &output, const Vector &v){
                    std::ios_base::fmtflags f = output.flags(std::ios::right | std::ios::scientific);
                    std::streamsize p = output.precision(16);
                    output<<"["
                      <<std::setw(23)
                      <<v._x<<", "
                      <<std::setw(23)
                      <<v._y<<", "
                      <<std::setw(23)
                      <<v._z<<"]";
                    output.flags(f);
                    output.precision(p);
                    return output;
                  } 
                  

                  最后,如果您绝对必须摆脱常量 23 的重复,您可以这样做(但我不推荐这样做):

                  Finally, if you absolutely have to get rid of the duplication of the constant 23, you could do something like this (but I wouldn't recommend it):

                  struct width {
                    int w;
                    width(int w) : w(w) {}
                    friend std::ostream& operator<<(std::ostream&os, const width& w) {
                      return os << std::setw(width.w);
                    }
                  };
                  
                  
                  std::ostream& operator<<(std::ostream &output, const Vector &v){
                    std::ios_base::fmtflags f = output.flags(std::ios::right | std::ios::scientific);
                    std::streamsize p = output.precision(16);
                    width w(23);
                    output<<"["
                      <<w
                      <<v._x<<", "
                      <<w
                      <<v._y<<", "
                      <<w
                      <<v._z<<"]";
                    output.flags(f);
                    output.precision(p);
                    return output;
                  } 
                  

                  另请参阅另一个问题,他们认为您不能将宽度设为永久.

                  See also this other question, where they decided that you can't make width permanent.

                  这篇关于C++ iomanip库的有效使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将整个流读入 std::string? 下一篇:std::ofstream,写入前检查文件是否存在

                  相关文章

                  <tfoot id='pi39M'></tfoot>

                  1. <small id='pi39M'></small><noframes id='pi39M'>

                      <i id='pi39M'><tr id='pi39M'><dt id='pi39M'><q id='pi39M'><span id='pi39M'><b id='pi39M'><form id='pi39M'><ins id='pi39M'></ins><ul id='pi39M'></ul><sub id='pi39M'></sub></form><legend id='pi39M'></legend><bdo id='pi39M'><pre id='pi39M'><center id='pi39M'></center></pre></bdo></b><th id='pi39M'></th></span></q></dt></tr></i><div id='pi39M'><tfoot id='pi39M'></tfoot><dl id='pi39M'><fieldset id='pi39M'></fieldset></dl></div>
                      <legend id='pi39M'><style id='pi39M'><dir id='pi39M'><q id='pi39M'></q></dir></style></legend>
                      • <bdo id='pi39M'></bdo><ul id='pi39M'></ul>