求complex在c++中的详细解释与用法

来源:百度知道 编辑:UC知道 时间:2024/05/18 15:48:01
求complex在c++中的详细解释与用法

complex库中的模板complex<>提供了一个复数类型,它与其他数值类型兼容。在早期的C++库中,complex并不是一个模板,它只是基于下列数据描述的一种类型:

class complex{

// ……methods

private:

double x, y;

};

现在complex则由以下模板描述:

template <class SCALAR>

class complex{

// ……methods

private:

SCALAR x, y;

};

这使用户能够根据需要决定基本类型的精度。一般来说,这些基本类型是float、double或者long double。下面是用于测试这个类型的简单代码。

文件 complex.cpp

#include <iostream>

#include <complex>

using namespace std;

int main()

{

complex<double> x(1,2.1), y;

cout << "x = " << x << endl;

y = x + 1.0;

cout << "\ny = " << y << endl;

// if (x < y) not allowed - no standard definition

// cout << "x less than y" &l