C++ Standard Library C++ STL Library

C++ <complex> - operator<< Function



The C++ <complex> operator<< function is used to write a complex number to output stream in the form (real,imaginary).

Syntax

template<class T, class charT, class traits>
  basic_ostream<charT,traits>&
    operator<< (basic_ostream<charT,traits>& ostr, 
                const complex<T>& rhs);

Parameters

ostr Specify a character output stream.
rhs Specify complex value to be inserted.

Return Value

ostr

Example:

The example below shows the usage of operator<< function with a complex number.

#include <iostream>
#include <complex>
using namespace std;
 
int main (){
  complex<double> z1 (10, 20);
  complex<double> z2 (2, -3);
  
  //write z1  and z2 to output stream
  cout<<"z1 : "<<z1<<"\n";
  cout<<"z2 : "<<z2<<"\n";

  return 0;
}

The output of the above code will be:

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

❮ C++ <complex> Library