使用fill()和fill_n()在2D双型数组中的错误 [英] error in using fill() and fill_n() in 2D double type array

查看:199
本文介绍了使用fill()和fill_n()在2D双型数组中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图使用fill()像下面的方式,它会导致我的错误:

  double dp [165 ] [2]; 
fill(dp,dp + 165 * 2,-1.0);

错误:宏fill传递了3个参数,但只有2个

它也导致错误,当我尝试使用fill_n():

  double dp [165] [2] 
fill_n(dp,165 * 2,-1.0)

错误:在'const double'到'double [2]'的赋值中不兼容类型


$ b

$ b <$>

$ b <$>

$ b <$>

  std :: fill(dp [0],dp [0] + 165 * 2,-1.0); 

  std :: fill_n(dp [0],165 * 2,-1.0); 

应该在标准C ++中工作。有两个参数的 fill 宏的错误表明,名为 #define / code>,干扰C ++标准库。这可能会打破比这更多的事情,所以你应该看看它,看看是否有像 NOMINMAX 宏阻止 windows.h min max < limit> )。



遇到的问题是 dp 不是 double double [2] code>无法分配。上面的代码使用了一个双精度数组在内存中看起来就像一个双精度数组,这意味着通过 dp double * ,它可以像1D数组一样填充。 dp [0] 只是获取指向 dp 类型<$ c的第一个元素的指针的最简单方法$ c> double *



附录:技术上, dp [0] double [2] ,但是当使用它时,它会衰减到 double * dp 只能衰减到 double(*)[2] (指向两个双精度数组的指针)


When i'm trying to use fill() like following way, it causing me error :

double dp[165][2];
fill(dp, dp+165*2, -1.0);

error: macro "fill" passed 3 arguments, but takes just 2

It also causing error when i'm trying with fill_n() :

double dp[165][2];
fill_n(dp, 165*2, -1.0);

error: incompatible types in assignment of 'const double' to 'double [2]'

can you point out the problem here ?

解决方案

Either

std::fill(dp[0], dp[0] + 165 * 2, -1.0);

or

std::fill_n(dp[0], 165 * 2, -1.0);

should work in standard C++. The error about fill macro with two arguments suggests that something #defines a macro named fill that interferes with the C++ standard library, though. This is likely to break more things than just this, so you should look into it and see if there's something like the NOMINMAX macro that prevents windows.h from doing the same with min and max (which breaks <limits>).

The problem you run into is that the value type ofdp is not double but double[2], to which -1.0 cannot be assigned. The above code uses the fact that a 2D array of doubles looks in memory just like a flat array of doubles, which means that by casting dp to double*, it can be filled just like a 1D array. dp[0] is just the easiest way to get a pointer to the first element of dp of type double*.

Addendum: Technically, dp[0] is of type double[2], but this decays to double* when it is used. dp can only decay to double (*)[2] (pointer to array of two doubles).

这篇关于使用fill()和fill_n()在2D双型数组中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆