Oracle Solaris Studio 12.2:C++ 用户指南

11.4.2 complex

标准库提供了模板化的 complex 库,该库与 C++ 4.2 编译器提供的 complex 库类似。如果在标准模式下编译,必须使用 <complex> 而非 <complex.h>。不能在兼容模式下使用 <complex>

在兼容模式下,必须在链接时显式请求 complex 库。在标准模式下,complex 库包括在 libCstd 中,缺省情况下链接该库。

标准模式下,没有 complex.h 头文件。在 C++ 4.2 中,"complex" 是类名称,但在标准 C++ 中,"complex" 是模板名称。不可能提供可使旧的代码不加修改就可工作的 typedef。因此,为使用复数的 4.2 版编写的代码需要某些简单的编辑,以便使用标准库。例如,以下代码是为 4.2 版编写的,并将在兼容模式下编译。


// file ex1.cc (compatibility mode)
#include <iostream.h>
#include <complex.h>

int main()
{
    complex x(3,3), y(4,4);
    complex z = x * y;
    cout << "x=" << x << ", y=" << y << ", z=" << z << endl;
}

以下示例在兼容模式下编译并链接 ex1.cc,然后执行该程序。


example% CC -compat ex1.cc -library=complex
example% a.out
x=(3, 3), y=(4, 4), z=(0, 24)

下面将 ex1.cc 重写为 ex2.cc 以在标准模式下编译:


// file ex2.cc (ex1.cc rewritten for standard mode)
#include <iostream>
#include <complex>
using std::complex;

int main()
{
     complex<double> x(3,3), y(4,4);
     complex<double> z = x * y;
     std::cout << "x=" << x << ", y=" << y << ", z=" << z <<
       std::endl;
}

以下示例在标准模式下编译并链接重写的 ex2.cc,然后执行该程序。


% CC ex2.cc
% a.out
x=(3,3), y=(4,4), z=(0,24)

关于使用复数运算库的更多信息,请参见表 13–4