C++ Standard Library C++ STL Library

C++ <complex> - operator*= Function

The C++ complex::operator*= function supports compound assignment operator (multiply AND assignment operator) of two complex numbers or a complex and a scalar.


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


val Specify scalar value of matching type.
rhs Specify complex value of matching type.

Return Value



In the example below, the complex::operator*= function is used to perform multiply and assignment operation on a given complex number.

#include <iostream>
#include <complex>
using namespace std;
int main (){
  complex<double> z1 (10, 20);
  complex<double> z2 (2, 3);
  double x = 0.5;
  //displaying z1
  cout<<"z1 : "<<z1<<"\n";
  //multiplying z1 by z2
  z1 *= z2;

  //displaying z1
  cout<<"z1 : "<<z1<<"\n";  

  //multiplying z1 by x
  z1 *= x;

  //displaying z1
  cout<<"z1 : "<<z1<<"\n";  

  return 0;

The output of the above code will be:

z1 : (10,20)
z1 : (-40,70)
z1 : (-20,35)

❮ C++ <complex> Library