operator<<,>>(std::complex)
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Numerics library
Mathematical special functions (C++17)
Mathematical constants (C++20)
Basic linear algebra algorithms (C++26)
Data-parallel types (SIMD) (C++26)
Floating-point environment (C++11)
Bit manipulation (C++20)
Saturation arithmetic (C++26)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
std::complex
(until C++20)
operator<<operator>>
(C++26)
(C++26)
(C++26)
Defined in header
<complex>
template< class T, class CharT, class Traits >
(1)
std::basic_ostream <CharT, Traits>&
template< class T, class CharT, class Traits >
(2)
std::basic_istream <CharT, Traits>&
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).[edit] Exceptions
May throw std::ios_base::failure on stream errors.
[edit] Parameters
os
-
a character output stream
is
-
a character input stream
x
-
the complex number to be inserted or extracted
[edit] Return value
1) os
2) is
[edit] 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.
[edit] 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(); }
[edit] Example
Run this code
#include <complex> #include <iostream> int main() { std::cout << std::complex <double> {3.14, 2.71} << '\n'; }
Possible output:
(3.14,2.71)