function
<complex>

std::complex operators

member functions
complex& operator= (const T& val);
complex& operator+= (const T& val);
complex& operator-= (const T& val);
complex& operator*= (const T& val);
complex& operator/= (const T& val);

complex& operator= (const complex& rhs);

template<class X> complex& operator= (const complex<X>& rhs);
template<class X> complex& operator+= (const complex<X>& rhs);
template<class X> complex& operator-= (const complex<X>& rhs);
template<class X> complex& operator*= (const complex<X>& rhs);
template<class X> complex& operator/= (const complex<X>& rhs);

non-member functions

template<class T> complex<T> operator+(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator+(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator+(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator-(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator-(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator-(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator*(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator*(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator*(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator/(const complex<T>& lhs, const complex<T>& rhs);
template<class T> complex<T> operator/(const complex<T>& lhs, const T& val);
template<class T> complex<T> operator/(const T& val, const complex<T>& rhs);

template<class T> complex<T> operator+(const complex<T>& rhs);
template<class T> complex<T> operator-(const complex<T>& rhs);

bool operator==(const complex<T>& lhs, const complex<T>& rhs);
bool operator==(const complex<T>& lhs, const T& val);
bool operator==(const T& val, const complex<T>& rhs);

bool operator!=(const complex<T>& lhs, const complex<T>& rhs);
bool operator!=(const complex<T>& lhs, const T& val);
bool operator!=(const T& val, const complex<T>& rhs);

template<class T, class charT, class traits>
  basic_istream<charT,traits>&
    operator>> (basic_istream<charT,traits>& istr, complex<T>& rhs);
template<class T, class charT, class traits>
  basic_ostream<charT,traits>&
    operator<< (basic_ostream<charT,traits>& ostr, const complex<T>& rhs);
Complex number operators
The complex class supports assignment and arithmetic operators between two complex objects and between a complex object and a numeric expression representing the real part of a complex number.

The <complex> header also includes the overload for extraction and insertion operations on input and output streams, respectively.

The format in which they are formatted for output insertion is (real,imag), while for input extraction they may be formatted as a real, as (real), or as (real,imag).

Parameters

val
Value representing a real numeric value without imaginary part.
T is the template parameter of class complex (i.e., its value type).
rhs
Right-hand side object of a complex type.
lhs
Left-hand side object of a complex type (only for non-member functions).
istr
Input stream object where the value of a complex object is inserted (see basic_istream).
ostr
Output stream object from where a complex value is extracted (see basic_ostream).

Template parameters

T
The template type (the value type) of the first complex object.
X
The template type (the value type) of the second complex object.
charT, traits
The template types for the basic_istream or basic_ostream object.

Return value

The left-hand side operand.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// complex operators
#include <iostream>     // std::cout
#include <complex>      // std::complex

int main ()
{
  std::complex<double> mycomplex;

  mycomplex = 10.0;   // 10
  mycomplex += 2.0;   // 12

  mycomplex = std::complex<double>(10.0,1.0);  // 10+i

  mycomplex = mycomplex + 10.0 ;               // 20+i

  if (mycomplex == std::complex<double>(20.0,1.0))
    std::cout << "mycomplex is " << mycomplex << '\n';

  return 0;
}


Output:

mycomplex is (20,1)

See also