当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。