C++ 中的 imag() 函数
c++server side programmingprogramming更新于 2025/6/27 10:07:17
在本文中,我们将讨论 C++ 中 imag() 函数的工作原理、语法和示例。
什么是 imag()?
imag() 函数是 C++ STL 中的一个内置函数,定义在 <complex> 头文件中。imag() 用于计算复数的虚部。
复数是由一个实数和一个虚数组合而成的数。实数是除无穷大和虚数之外的任何数字。
虚数是指平方为负数的数。该函数返回虚部,虚部是与虚数单位相乘的因数。
语法
Template <class T> T imag(const complex<T>& 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