C++ 在C++;

C++ 在C++;,c++,data-structures,struct,constructor,C++,Data Structures,Struct,Constructor,我正在做“生活游戏”作为学校的一项作业,导师给了我们部分代码,但我在理解两部分方面有问题 第一: grid(int scale) : scale_(scale), width_(problem_definition::width(scale)), height_(problem_definition::height(scale)), data_(new problem_definition::data_type[width_*height_]) 直到我能理解的时候,他才请求内存,用于模拟网格

我正在做“生活游戏”作为学校的一项作业,导师给了我们部分代码,但我在理解两部分方面有问题

第一:

grid(int scale) : scale_(scale), width_(problem_definition::width(scale)), height_(problem_definition::height(scale)), data_(new problem_definition::data_type[width_*height_]) 
直到我能理解的时候,他才请求内存,用于模拟网格。但是,我无法掌握新语法以外的内容。为什么那些括号中有空格?网格后的冒号(整数比例)是什么意思

第二:

void initialize()
{
for (std::size_t y = 0; y < height_; ++y)
{
    for (std::size_t x = 0; x < width_; ++x)
    {
        at(x,y) = problem_definition::initialize(x, y, scale_);
    }
}
void initialize()
{
对于(标准::尺寸y=0;y<高度++y)
{
对于(标准::尺寸x=0;x
}

第二个for循环中的“at()”是什么

如果您能了解完整的结构定义,我将不胜感激:

struct grid
{
  grid(int scale) : scale_(scale), width_(problem_definition::width(scale)), height_(problem_definition::height(scale)), data_(new problem_definition::data_type[width_*height_])
{

}
void initialize()
{
    for (std::size_t y = 0; y < height_; ++y)
    {
        for (std::size_t x = 0; x < width_; ++x)
        {
            at(x,y) = problem_definition::initialize(x, y, scale_);
        }
    }
}
problem_definition::data_type at(std::size_t x, std::size_t y, int dx, int dy)
{
    if (dx<0 && -dx>x) return problem_definition::out_of_bounds_value();
    if (dy<0 && -dy>y) return problem_definition::out_of_bounds_value();
    if (x + dx >= width_) return problem_definition::out_of_bounds_value();
    if (y + dy >= height_) return problem_definition::out_of_bounds_value();
    return at((int)x + dx, (int)y + dy);
}
problem_definition::data_type& at(std::size_t x, std::size_t y)
{
    return data_[x + y*width_];
}
void swap(grid& other)
{
    std::swap(scale_, other.scale_);
    std::swap(width_, other.width_);
    std::swap(height_, other.height_);
    std::swap(data_, other.data_);
}
void print(std::ostream& str)
{
    for (std::size_t y = 0; y < height_; ++y)
    {
        for (std::size_t x = 0; x < width_; ++x)
        {
            str << at(x, y);
        }
        str << std::endl;
    }
}
private:
 int scale_;
 std::size_t width_;
 std::size_t height_;
 std::unique_ptr<problem_definition::data_type[]> data_;
};
problem_definition::data_type& at(std::size_t x, std::size_t y)
{
    return data_[x + y*width_];
}
结构网格 { 网格(整数比例):比例(比例),宽度(问题定义::宽度(比例)),高度(问题定义::高度(比例)),数据(新问题定义::数据类型[宽度*高度]) { } void initialize() { 对于(标准::尺寸y=0;y<高度++y) { 对于(标准::尺寸x=0;x=width)返回问题定义::超出界限值(); 如果(y+dy>=高度)返回问题定义::超出界限值(); 在((int)x+dx,(int)y+dy)返回; } 问题定义::数据类型和at(标准::大小x,标准::大小y) { 返回数据[x+y*宽度]; } 无效交换(电网和其他) { std::swap(scale\ux、other.scale\ux); std::swap(width_u,other.width_u); 标准::交换(高度,其他高度); std::swap(数据、其他数据); } 无效打印(标准::ostream和str) { 对于(标准::尺寸y=0;y<高度++y) { 对于(标准::尺寸x=0;xstr让我们在第一行中添加一些空格,以便于阅读:

grid(int scale) : scale_(scale),
                  width_(problem_definition::width(scale)),
                  height_(problem_definition::height(scale)),
                  data_(new problem_definition::data_type[width_*height_]) 
现在我们可以很容易地看到冒号(a
是冒号,a
是分号)定义了一个初始化器列表,它接受一系列
var(value)
对,并将每个
分配给每个
var

我没有看到任何“带空格的括号”,因此无法回答。但是beyond
new
只是一种标准的数组类型:
元素类型[length]
。元素类型是
问题定义::数据类型
,数组的长度
宽度
高度

最后,
at()
只是一个返回引用的函数调用。如果研究函数定义:

struct grid
{
  grid(int scale) : scale_(scale), width_(problem_definition::width(scale)), height_(problem_definition::height(scale)), data_(new problem_definition::data_type[width_*height_])
{

}
void initialize()
{
    for (std::size_t y = 0; y < height_; ++y)
    {
        for (std::size_t x = 0; x < width_; ++x)
        {
            at(x,y) = problem_definition::initialize(x, y, scale_);
        }
    }
}
problem_definition::data_type at(std::size_t x, std::size_t y, int dx, int dy)
{
    if (dx<0 && -dx>x) return problem_definition::out_of_bounds_value();
    if (dy<0 && -dy>y) return problem_definition::out_of_bounds_value();
    if (x + dx >= width_) return problem_definition::out_of_bounds_value();
    if (y + dy >= height_) return problem_definition::out_of_bounds_value();
    return at((int)x + dx, (int)y + dy);
}
problem_definition::data_type& at(std::size_t x, std::size_t y)
{
    return data_[x + y*width_];
}
void swap(grid& other)
{
    std::swap(scale_, other.scale_);
    std::swap(width_, other.width_);
    std::swap(height_, other.height_);
    std::swap(data_, other.data_);
}
void print(std::ostream& str)
{
    for (std::size_t y = 0; y < height_; ++y)
    {
        for (std::size_t x = 0; x < width_; ++x)
        {
            str << at(x, y);
        }
        str << std::endl;
    }
}
private:
 int scale_;
 std::size_t width_;
 std::size_t height_;
 std::unique_ptr<problem_definition::data_type[]> data_;
};
problem_definition::data_type& at(std::size_t x, std::size_t y)
{
    return data_[x + y*width_];
}
然后,通过引用的魔力,您可以看到调用
at()
的行与以下内容相同:

data_[x + y*width_] = problem_definition::initialize(x, y, scale_);

构造函数后面的冒号和后续代码是一个。它初始化类的字段,即为它们赋值。at()只是一个方法,返回对网格数据中元素的引用,即数据的简写[x+y*width]这是一个相当复杂的问题。在这个网站上,你最好问一个定义明确的问题,而不是寻求开放式的帮助。谢谢你的回答,我更正了问题中的冒号(分号)。