C++ Standard Library C++ STL Library

C++ <complex> - operator>> Function



The C++ <complex> operator>> function is used to read a complex number from input stream. The supported formats are:

  • real
  • (real)
  • (real,imaginary)

Syntax

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

Parameters

istr Specify a character input stream.
rhs Specify complex value to be extracted.

Return Value

istr

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;
  complex<double> z2;
  complex<double> z3;
  
  //takes complex numbers as inputs 
  //from input stream
  cin>>z1;
  cin>>z2;
  cin>>z3;

  //write z1, z2 and z3 to output stream
  cout<<"z1 : "<<z1<<"\n";
  cout<<"z2 : "<<z2<<"\n";
  cout<<"z3 : "<<z3<<"\n";

  return 0;
}

If the following inputs are given to the program:

10
(15)
(20, 50)

The output will be:

z1 : (10,0) 
z2 : (15,0)
z3 : (20,50)

❮ C++ <complex> Library

5