operator<< function template — Writes a complex number
template<typename T, typename charT, typename traits>
basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&,
const complex<T>& z);
The <<
operator
prints z
to the output stream in
the form (x, y)
, in which
x
is the real part, and y
is the imaginary part. Example 13-6 shows how z
is formatted. If you want more control
over the formatting, you must print the value yourself.
Example 13-6. Formatting a complex number
template<class T, class charT, class traits> std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& o, const std::complex<T>& x) { std::basic_ostringstream<charT, traits> s; s.flags(o.flags( )); s.imbue(o.getloc( )); s.precision(o.precision( )); s << "(" << x.real( ) << "," << x.imag( ) << ")"; return o << s.str( ); }