A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://en.cppreference.com/w/cpp/../cpp/io/ios_base/../../numeric/complex/operator_ltltgtgt.html below:

operator<<,>>(std::complex) - cppreference.com

(1) (2)

1) Writes to os the complex number in the form (real, imaginary).

2)

Reads a complex number from

is

. The supported formats are

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
#include <complex>
#include <iostream>
 
int main()
{
    std::cout << std::complex<double> {3.14, 2.71} << '\n';
}

Possible output:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4