c++ - 如何用 NaN 元素初始化双矩阵?

标签 c++ c++11 nan

在我的代码中,我有一个像这样的 double 矩阵:

double * * matrix=new double * [10];
for(int i=0;i<10;i++) 
    matrix[i]=new double[10];

当我初始化它时,我想在这个矩阵的每个单元格中都有 NaN 值,是否可以自动执行或唯一的解决方案是:

for(int i=0;i<10;i++)
    for(int j=0;j<10;j++)
        matrix[i][j]=nan("");

是否有可能推断出当矩阵构造时,它不使用 double 的默认构造函数插入,对于每个矩阵 [i][j],0.0 值但插入 nan("")?

最佳答案

double 没有默认构造函数,即 double 值默认未初始化。

要避免显式实现循环,您可以使用 std::vector :

#include <vector>
...
std::vector<std::vector<double>> matrix(10, std::vector<double>(10, nan("")));

或:

#include <vector>
using namespace std;
...
vector<vector<double>> matrix(10, vector<double>(10, nan("")));

关于c++ - 如何用 NaN 元素初始化双矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50659902/

相关文章:

c++ - 将 boost::chrono 计时机制包装到类编译错误中?

c++ - 使用 std::mutex 实现信号量

c++ - 可变参数模板的歧义

c - f(x) := if x == 0 then 0 else (x * log(x)) 的无分支实现

Java isNan 是如何工作的?

javascript - NaN 错误 - 处理字符串和整数

c++ - 将 JPEG 解码为 char*

c++ - bloom_filter_prl.exe : 0xC00000FD: Stack overflow 中 0x013f3277 的未处理异常

C++ : copy constructor that uses a function with non-const parameters

c++ - 如何在OpenCV中比较两个Mats列