如何从初始化列表中分配一个数组?

How can I assign an array from an initializer list?

我对 c++ 的了解有限。我试图编译一个 c++ 库,当我 运行 生成以下头文件的 make 文件时

mcmc_dhs.h

#include <algorithm>
#include <map>

// intrinsic shape and (reduced) shear just add?
//#define WLNOISE

// use shear instead of reduced shear for model
//#define NOREDSHEAR

/// parameters for the M200-concentration relation
const number mcreal[2] = {9.59,-0.102}; // Dolag et al. (2004)
//const number mcreal[2] = {5.26,-0.100}; // Neto et al. (2007) [Millenium Run]

/// critical density at z=0 (h100=1) in [Msun/Mpc^3]
const number rhocrit = exp(log(rhoCrit)+3.*log(Mpc)-log(Msun)); 

/// two extra halo parameters: r200 (and concentration: 2)
#define PARAMS 1

/// define region (square; twice value here) about halo that considers sources for model
#define REGION 10.0*arcmin

class mcmc_dhs : public mcmc
{
 public:

  mcmc_dhs() : 
  data(), cosmohandler(0.3,0.7,0.21,0.8,0.04),
    lenseff(), intrvar()
    {
      boundaries = 
    {{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}};
    }
  ~mcmc_dhs() {}

  /// size of grid for looking up sources
  static const int Ngrid = 200;

它returns以下错误信息:

mcmc_dhs.h:55:67: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
      boundaries = {{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}};
                                                                   ^
mcmc_dhs.h:55:17: error: assigning to an array from an initializer list
      boundaries = {{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}};
                 ^
In file included from ../modules/matrix.h:15:0,
                 from ../modules/probdensity.h:4,
                 from ../modules/mcmc.h:4,
                 from mcmc_dhs.h:4,

不能在数组声明后直接赋值给它。基本上你的代码是一样的

int main()
{
    double arr[2][2];
    arr = { {1, 2}, {3, 4.5} }; // error
}

您必须在声明时分配值

double arr[2][2] = { {1, 2}, {3, 4.5} };

或使用循环(或std::copy)来分配元素。由于你的数组好像是成员变量,你也可以在构造函数初始化列表中初始化:

 mcmc_dhs() : data(), cosmohandler(0.3,0.7,0.21,0.8,0.04), 
              lenseff(), intrvar(), 
              boundaries{{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}}
 { 
    // rest of ctor implementation
 }

当你说

boundaries = 
{{0,512},{0,512},{0.01,5.},{100.,3000.},{0.1,50}};

这是不正确的,因为 C++ 不允许您重新分配数组值。有一个简单的解决方法,但有点乏味。您所要做的就是一个一个地赋值。

例如:

boundaries[0][0] = 0;
boundaries[0][1] = 512;
boundaries[1][0] = 0;
boundaries[1][1] = 512;

等等。我在 Arduino 程序中遇到了同样的问题。

数组本质上只是指针。 C++(是一种基于符号的编程语言)对数组有自己的解释。含义:

int* a[3];您已经声明了数组,但目前分配给每个元素的值是一些垃圾值,这些值已经存储在分配给您的数组的内存位置。

a={1,2,3};将不起作用,因为:C++ 将数组名称 'a' 视为指向数组中第一个元素的地址位置的指针。 'a' 基本上被 C++ 解释为 '&a[0]' 这是元素 a[0]

的地址

因此,您有 2 种方法来分配值

  1. 使用数组索引(如果您不知道什么是指针,这是唯一的选择)

    int a[3]; for(int i=0;i<3;++i) // 使用for循环为每个元素赋值 { cin>>a[i]; }

2 把它当作一个指针,用指针操作

    int a[3];
    for(int i=0;i<3;++i) // using for loop to assign every element a value
    {
    cin>>*(a+i); // store value to whatever it points at starting at (a+0) upto (a+2)
    }

注意:不能使用 ++a 指针操作,因为 ++ 会改变指针的位置,而 a+i 不会改变指针的位置 'a',无论如何使用 ++ 都会给出一个编译器错误。

推荐阅读 Stephen Davis C++ for dummies 书。