C++ Standard Library C++ STL Library

C++ <complex> - operator- Function

The C++ <complex> operator- function is used to apply unary minus operator to a complex number.


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


rhs Specify right-hand side complex number.

Return Value

Returns a new complex number as a result of performing operation.


The example below shows the usage of operator- function.

#include <iostream>
#include <complex>
using namespace std;
int main (){
  complex<double> z1 (10, 20);
  complex<double> z2;

  //result of +z1 is stored in z2 
  z2 = -z1;

  //displaying the result
  cout<<"z1 : "<<z1<<"\n";
  cout<<"z2 : "<<z2<<"\n";
  return 0;

The output of the above code will be:

z1 : (10,20)
z2 : (-10,-20)

❮ C++ <complex> Library