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

< cpp‎ | numeric‎ | complex

Numerics library
Common mathematical functions
Mathematical special functions (C++17)
Mathematical constants (C++20)
Basic linear algebra algorithms (C++26)
Floating-point environment (C++11)
Complex numbers
Numeric arrays
Pseudo-random number generation
Factor operations
(C++17)
(C++17)
Interpolations
(C++20)
(C++20)
Saturation arithmetic
 mul_sat(C++26) div_sat(C++26)
Generic numeric operations
 iota(C++11) ranges::iota(C++23) accumulate inner_product adjacent_difference partial_sum
 reduce(C++17) transform_reduce(C++17) inclusive_scan(C++17) exclusive_scan(C++17) transform_inclusive_scan(C++17) transform_exclusive_scan(C++17)
Bit operations
 has_single_bit(C++20) bit_cast(C++20) bit_ceil(C++20) bit_floor(C++20) bit_width(C++20) rotl(C++20) rotr(C++20)
 popcount(C++20) countl_zero(C++20) countl_one(C++20) countr_zero(C++20) countr_one(C++20) byteswap(C++23) endian(C++20)

std::complex
Member functions
Non-member functions
 operator+operator- operator+operator-operator*operator/ operator==operator!=(until C++20) operator<> get(std::complex)(C++26)
 real imag abs arg norm conj proj(C++11) polar operator""ioperator""ifoperator""il(C++14)(C++14)(C++14)
Exponential functions
 log
 log10
Power functions
 pow
Trigonometric functions
 sin  cos tan
 asin(C++11) acos(C++11) atan(C++11)
Hyperbolic functions
 sinh  cosh tanh
 asinh(C++11) acosh(C++11) atanh(C++11)
Helper types

 Defined in header  template< class T, class CharT, class Traits > std::basic_ostream&     operator<<( std::basic_ostream& os, const std::complex& x ); (1) template< class T, class CharT, class Traits > 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(); }

### Example

#include <complex>
#include <iostream>

int main()
{
std::cout << std::complex<double> {3.14, 2.71} << '\n';
}

Possible output:

(3.14,2.71)