當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ imag()用法及代碼示例


imag()函數在複雜頭文件中定義。 imag()函數用於查找複數的虛部。

用法:

template<class T> T 
    imag(const complex<T>& z);

參數:此方法采用強製性參數z:,它表示給定的複數。


返回:它返回複數的虛部。

以下示例程序旨在說明上述函數:-

示例1:

// C++ program to demonstrate 
// example of imag() function. 
  
#include <bits/stdC++.h> 
using namespace std; 
  
// Driver function 
int main() 
{ 
  
    // defines the complex number: (20.3 + 4.9i) 
    complex<double> imagpart(20.3, 4.9); 
  
    // print the complex number 
    cout << "Complex Number = "
         << imagpart << endl; 
  
    // prints the imag part using the imag function 
    cout << "Imag part of the complex number is ="
         << imag(imagpart) << endl; 
  
    return 0; 
}
輸出:
Complex Number = (20.3,4.9)
Imag part of the complex number is =4.9

示例2:

// C++ program to demonstrate 
// example of imag() function. 
  
#include <bits/stdC++.h> 
using namespace std; 
  
// driver function 
int main() 
{ 
    // defines the complex number: (2 + 2i) 
    complex<double> imagpart(2, 2); 
  
    // print the complex number 
    cout << "Complex Number = "
         << imagpart << endl; 
  
    // prints the imag part using the imag function 
    cout << "Imag part of the complex number is ="
         << imag(imagpart) << endl; 
  
    return 0; 
}
輸出:
Complex Number = (2,2)
Imag part of the complex number is =2


相關用法


注:本文由純淨天空篩選整理自bansal_rtk_大神的英文原創作品 imag() function in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。