operator<<,>>(std::complex)

< cpp‎ | numeric‎ | complex

C++
 Language Headers Library concepts Language support library Diagnostics library Utilities library Strings library Containers library Algorithms library Iterators library Numerics library Input/output library Localizations library Regular expressions library (C++11) Atomic operations library (C++11) Thread support library (C++11) Filesystem library (C++17) Technical Specifications

Numerics library
 Common mathematical functions Special mathematical functions Floating-point environment (C++11) Complex numbers Numeric arrays Pseudo-random number generation Compile-time rational arithmetic (C++11) Numeric algorithms gcd(C++17) lcm(C++17) Generic numeric operations iota(C++11) accumulate inner_product adjacent_difference partial_sum

std::complex
Member functions
Non-member functions
 operator+operator- operator+operator-operator*operator/ operator==operator!= operator<>
 real imag abs arg norm conj proj(C++11) polar operator""ioperator""ifoperator""il(C++14)(C++14)(C++14)
Exponential functions
Power functions
Trigonometric functions
 asin(C++11) acos(C++11) atan(C++11)
Hyperbolic functions
 asinh(C++11) acosh(C++11) atanh(C++11)

 Defined in header  template std::basic_ostream&     operator<<(std::basic_ostream& os,                const std::complex& x); (1) template std::basic_istream&     operator>>(std::basic_istream& is,                std::complex& x); (2)
1) Writes to os the complex number in the form (real,imaginary).
2) Reads a complex number from is. 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

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 basic_ostream& operator<<(basic_ostream& o, const complex& x) { basic_ostringstream s; s.flags(o.flags()); s.imbue(o.getloc()); s.precision(o.precision()); s << ’(’ << x.real() << "," << x.imag() << ’)’; return o << s.str(); }