更改复数输出格式 < C++ >标准库中有复合体< /C> >模板,它有超载

更改复数输出格式 < C++ >标准库中有复合体< /C> >模板,它有超载,c++,complex-numbers,C++,Complex Numbers,没有直接替换代码< >运算符>代码>模板的方法 结构我的\u复杂\u格式\u类型{ std::复杂常数&x; my_complex_format_type(std::complex const&x):x(x){} friend std::ostream&operator没有真正整洁的方法可以做到这一点。我的建议是抛弃iostreams,编写更像C的东西。它可能会更快地编写、更快地读取和更快地执行。对于任何复杂的的具体实例化,请使用强类型定义(boost有一个版本)在我对同一个问题的回答中,转换为

没有直接替换代码< >运算符>代码>模板的方法 结构我的\u复杂\u格式\u类型{ std::复杂常数&x; my_complex_format_type(std::complex const&x):x(x){}
friend std::ostream&operator没有真正整洁的方法可以做到这一点。我的建议是抛弃iostreams,编写更像C的东西。它可能会更快地编写、更快地读取和更快地执行。

对于任何复杂的
的具体实例化,请使用强类型定义(boost有一个版本)在我对同一个问题的回答中,转换为这种类型:产生你想要的行为,但要冒将来不兼容的风险,因为它在
std::
命名空间中插入了模板专门化。

到什么?你想截断数字还是想打印
pears!
每次,你都需要d来具体说明。并不是说这不是一个很好的答案,但这不正是我几分钟前发布的吗?:-)@templatetypedef:我开始阅读这个问题,然后在你发布前几分钟键入我的答案,然后在你发布后67秒才点击提交。这是个问题吗?@Fred Nurk-我的道歉,这不是故意的对抗性。事实上,我很高兴我们得到了相同的答案……这意味着这可能是一个非常好的设计!@templatetypedef:我只是不明白你为什么要“指出”(或“指责”,尽管我在第一条评论中没有解释你太具对抗性)这是一个不错的设计:它是所有有状态操纵器(无状态操纵器,如endl,是函数)的通用模式“Fred Nurk,你说得对,我和那个评论不太一致。我当然不想在我们之间开始敌对行动,我肯定不会再这么做了。至于这个特殊的模式,我以前从未见过它,虽然我可以看到它确实有用。我定期教C++编程课程,我可能会。当谈到操作符重载时,把它作为一个很酷的例子来说明它是多么灵活。谢谢!这正是我需要的。实际上有一种方法可以取代<代码>操作符+ 1:C++输出格式的臭味。我记得在Tc++PL中引入IO流的方式,Stroustrup从一开始就告诉说编写一个库是不容易的。这会让每个人都高兴。这或多或少像是在开玩笑,说可能不是每个人都会喜欢它…@6502:我同意iostreams很臭,但这个用例实际上很整洁。-1,std::complex是一个经典的例子,其中
printf()Fred Nurk:很明显,TyDyess在观察者的眼中。我考虑<代码> PrimTf(“%0.6ft%0.6f”,双(x.RealEnter),双(x.IMAGE()));这么干净,你的15行模糊样板,甚至实际上不做格式化。例如,添加一些“%0.6f”的一些真正的(IMO)。可怕的东西。注意,我当然不是说这是你的错…只是C++格式很烂。这种代码写起来比较难,读起来更困难,编译时间长,生成更大的程序,运行慢。
template <typename T> void PrintComplex(const complex<T>& c) {
    /* ... */
}
template <typename T> class ComplexPrinter {
public:
    /* Conversion constructor allows for implicit conversions from
     * complex<T> to ComplexPrinter<T>.
     */
    ComplexPrinter(const complex<T>& value) : c(value) {
        // Handled in initializer list
    }

    /* Output the complex in your own format. */
    friend ostream& operator<< (ostream& out, const ComplexPrinter& cp) {
        /* ... print in your own format ... */
    }

private:
    complex<T> c;
};
cout << ComplexPrinter<double>(myComplex) << endl;
template <typename T>
ComplexPrinter<T> wrap(const complex<T>& c) {
    return ComplexPrinter<T>(c);
}
cout << wrap(myComplex) << endl;
vector< complex<double> > v = /* ... */
copy (v.begin(), v.end(), ostream_iterator< ComplexPrinter<double> >(cout, " "));
template <typename T> class ComplexPrinter {
public:
    /* Conversion constructor allows for implicit conversions from
     * complex<T> to ComplexPrinter<T>.
     */
    ComplexPrinter(const complex<T>& value) : c(value) {
        // Handled in initializer list
    }

    /* Output the complex in your own format. */
    friend ostream& operator<< (ostream& out, const ComplexPrinter& cp) {
        /* ... print in your own format ... */
    }

private:
    const complex<T>& c;
};
template<class T>
struct my_complex_format_type {
  std::complex<T> const &x;
  my_complex_format_type(std::complex<T> const &x) : x (x) {}
  friend std::ostream& operator<<(std::ostream &out,
                                  my_complex_format_type const &value)
  {
    out << "format value.x however you like";
    return out;
  }
};
template<class T>
my_complex_format_type<T> my_complex_format(std::complex<T> const &x) {
  return x;
}

void example() {
  std::cout << my_complex_format(some_complex);
}