C++ complex numbers, what is the right format?

前端 未结 5 506
闹比i
闹比i 2020-12-28 08:59

I want to use C++ with complex numbers. Therefore I included #include . Now my question is: How do I declare a variable?(so what is the format ca

5条回答
  •  遥遥无期
    2020-12-28 09:25

    You define a variable by specifying a template parameter and specifying a name for the variable, about like with most other templates:

    std::complex x(1, 1);
    

    The first parameter to the ctor is the real part, the second the imaginary part.

    Starting with C++ 14, a user-defined literal operator has been added, so you can initialize a complex variable with a somewhat more natural notation:

    using namespace std::literals;
    
    std::complex c = 1.2 + 3.4i;
    

    In this case, (obviously enough) the 1.2 is the real part and the 3.4 is the imaginary part.

提交回复
热议问题