function

std::complex operators

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

complex& operator= (const complex& rhs);

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

global 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);

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, 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 a (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 (used in global functions).
istr
Input stream object where the value of a complex object is inserted (see istream).
ostr
Output stream object from where a complex value is extracted (see 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 objects.

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
21
// complex constructor example
#include <iostream>
#include <complex>
using namespace std;

int main ()
{
  complex<double> mycomplex;

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

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

  mycomplex = mycomplex + 10.0 ; // 20+i

  if (mycomplex == complex<double>(20.0,1.0))
	  cout << "mycomplex is " << mycomplex << endl;

  return 0;
} 


Output:

mycomplex is (20,1)

See also