Namespaces
Variants
Views
Actions

std::operator<<,>>

From cppreference.com
< cpp‎ | numeric‎ | complex
Revision as of 02:44, 30 November 2012 by Bazzy (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <complex>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template <class T, class CharT, class Traits>

std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,

               const std::complex<T>& x);
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template <class T, class CharT, class Traits>

std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& is,

               std::complex<T>& x);
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

1) Writes to os the complex number in the form (real,imaginary).
2) Reads a complex number from os. The supported formats are
  • real
  • (real)
  • (real,imaginary)

Where the input for real and imaginary must be convertible to T.

If an error occurs calls is.setstate(ios_base::failbit)

Contents

Exceptions

May throw std::ios_base::failure on stream errors.

Parameters

os - a character output stream
is - a character input stream
x - the complex number to be inserted or extracted

Return value

1) os
2) is

Notes

1) As the comma may be used in the current locale as decimal separator, the output may be ambiguous. This can be solved with std::showpoint which forces the decimal separator to be visible.
2) The input is performed as a series of simple formatted extractions. Whitespace skipping is the same for each of them.

Possible implementation

template<class T, class charT, class traits>
basic_ostream<charT, traits>& 
operator<<(basic_ostream<charT, traits>& o, const complex<T>& x) {
    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();
}