c++ - 如何在不混淆字段的情况下声明复数?

标签 c++

<分区>

我尝试创建复数,但无论出于何种原因,它们总是出现具有虚值的实部和虚部归零。 例如,我希望这个小测试应用程序打印 (1,2) 的多个实例,但无论我如何尝试接近它,我得到的都是 (2,0)。

#include "stdafx.h"
#include <complex>
#include <iostream>
using namespace std;
int main()
{
    std::complex<double>** complexdouble_c;
    complexdouble_c = new std::complex<double>*[5];
    for (int i = 0; i < 5; ++i)
        complexdouble_c[i] = new std::complex<double>[2];

    for (int i = 0; i < 5 && i < 15; i++) {
        for (int j = 0; j < 2 && j < 15; j++) {
                double real = 1;
                double imag = 2;
                complexdouble_c[i][j] = (real, imag);
            }
        }
    for (int i = 0; i < 5 && i < 15; i++) {
        for (int j = 0; j < 2 && j < 15; j++) {
            std::cout << complexdouble_c[i][j];
        }
        std::cout << "\n";
    }
    std::complex<double> complexdouble;
    double real = 1.0;
    double imag = 2.0;
    complexdouble = (real, imag);
    std::cout << complexdouble;
    std::cout << complexdouble.real();
    std::cout << complexdouble.imag();
    getchar();
}

最佳答案

以这种格式声明。这将给出以下输出

std::complex<double> c(1, 2);
// 1 + 2i

关于c++ - 如何在不混淆字段的情况下声明复数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59062224/

相关文章:

c++ - `enum class` 的冗余语法

c++ - fmod函数的输出c++

c++ - 在指向(全局/持久)对象的指针超出范围后,对象(不是指针)的成员 vector 获取 'unset'

c++ - 如何给一个枚举值的索引并得到它?

c++ - 类成员变量

c++ - 是否有像 C++ std set 这样的数据结构也可以快速返回范围内的元素数量?

冒泡排序的 C++ 问题

c++ - 为什么那个类型是 double 而不是 float?

c++ - 在函数内声明 const 而不是变量有什么好处吗?

c++ - 我是否必须使用 std::shared_ptr 删除对对象的所有引用