C++ 中的 imag() 函数


本文将讨论 C++ imag() 函数的工作原理、语法和示例。

什么是 imag()?

imag() 函数是 C++ STL 中的内置函数,在 <complex>头文件中定义。imag() 用于查找复数的虚部。

复数是由实数和虚数组合而成的数。实数是指除了无穷大和虚数之外的任何数。

虚数是指它们的平方为负数的数。函数返回虚部,虚部是虚数单位被乘以的因子。

语法

Template <class T> T imag(const complex<T>& num);

参数

该函数接受以下参数:

  • num - 这是给定的复数。

返回值

此函数返回 num 的虚部。

输入 

complex<double> img(2.2,3.4);
imag(img);

输出 

3.4

示例

 实时演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   //complex number = (a + ib)
   complex<double> img(2.2,3.4);
   cout<<"The complex number is: "<<img;
   cout<<"\nThe Imaginary part of the complex number is: "<<imag(img) << endl;
   return 0;
}

输出

如果我们运行上述代码,它将生成以下输出:

The complex number is: (2.2,3.4)
The Imaginary part of the complex number is: 3.4

示例

 实时演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   //complex number = (a + ib)
   complex<double> img(32,12);
   cout<<"The complex number is: "<<img;
   cout<<"\nThe Imaginary part of the complex number is: "<<imag(img) << endl;
   return 0;
}

输出

如果我们运行上述代码,它将生成以下输出:

The complex number is: (32,12)
The Imaginary part of the complex number is: 12

更新于: 2020 年 4 月 22 日

154 次浏览

开始您的 职业

完成课程,获得认证

开始
广告
© . All rights reserved.