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


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


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

用法:

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

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


返回:它返回指定複數的實部。

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

示例1:

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

示例2:

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


相關用法


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