在数组中一次分配整个结构

Assign a whole struct at once in an array

本文关键字:分配 一次 结构 数组      更新时间:2023-10-16

我正在学习C++,但我对struct和数组有问题。我的结构是:

struct board
{
    string name;
    string desc;
    int nval;
    int sval;
    int eval;
    int wval;
};

我的数组如下所示:

board field[10][10];

例如,我可以做到:

field[5][6].name = "ExampleName";
field[5][6].desc = "This is an example";
field[5][6].nval = 3;
//and so on...

但是我想立即分配给整个结构,如下所示:

field[5][6] = {"ExampleName", "This is an example", 3, 4, 5, 6};
//so I don't have to type everything over and over again...

你尝试做的事情在 C 标准中是允许的。它似乎也在C++工作。我在 C++11 中验证了它:

struct board
{
string name;
string desc;
int nval;
int sval;
int eval;
int wval;
}field[10][10];
int main()
{    
field[5][6]={"ExampleName","This is an example",3,4,5,6};
cout<<field[5][6].name<<endl;
cout<<field[5][6].sval<<endl;
return 0;
}

它打印正确。所以你应该能够做到这一点。

如前所述,C99 和 C# 支持该语法的一种形式,但标准C++不支持。您可以通过向结构中添加构造函数来做到这一点。请注意,这将不再与 ANSI C 兼容。

struct board
{
    string name;
    string desc;
    int nval;
    int sval;
    int eval;
    int wval;
    board()
    {
    }
    board(string name, string desc, int nval, int sval, int eval, int wval)
    {
        this->name = name;
        this->desc = desc;
        this->nval = nval;
        this->sval = sval;
        this->eval = eval;
        this->wval = wval;
    }
};

field[1][2] = board("name", "desc", 1, 2, 3, 4);

I'm afraid you can't do it this way.

But in real life it's not a problem because you don't normally need to fill this kind of fields manually. You usually do it in a loop.

In case you wouldn't mind runtime initialization, I'd do it this way:

    // in the beginning make these arrays
string names[10*10] = {
    "example 1 name"
    "example 2 name"
    "blah blah blah "
};
string descriptions[100] = {
};
//and then just loop through that
int i,j;
for (int k = 0; k != 10*10; ++k) { // 10*10 size of a board
        if (j == 10) {
            j = 0;
            i++
        }
        field[i][j].name = names[k]// get it from names
        field[i][j].desc = // get somehow the description,...
        ++j
    }
}

如果您定义接受参数的构造函数,您将能够创建临时并使用它初始化给定元素。您还需要定义和默认构造函数:

struct board
{
   string name;
   string desc;
   int nval;
   int sval;
   int eval;
   int wval;
   board():
     name(""),
     desc(""),
     nval(0),
     sval(0),
     eval(0),
     wval(0){}
   board(
     const string& name,
     const string& desc,
     int nval,
     int sval,
     int eval,
     int wval):
   name(name),
   desc(desc),
   nval(nval),
   sval(sval),
   eval(eval),
   wval(wval){}
};
int main()
{
   board field[10][10];
   field[5][6]= board("ExampleName","This is an example",3,4,5,6);
   return 0;
}

如果您确实需要为所有 100 个字段定义精心挑选的值,您可以通过将所有参数写入文本文件,然后解析该文件并用提取的值填充数组来简化它。该文件可能如下所示

0 0
Corner field
Here it begins
0 1 2 3
0 1
ExampleName
This is an example
3 4 5 6

等等。然后,在读取文件时,您可以使用istream::getline提取文本字符串,istream::operator>>提取数字。

但这仍然很痛苦。您确定没有自动方法可以生成至少大部分值吗?

这个问题不太清楚你用一行是什么意思,所以我将开始提供建议:

使用聚合初始化:

board fields[10][10] = { 
                  { {"ExampleName","This is an example",3,4,5,6}, // Element 0,0
                    {"ExampleName","This is an example",3,4,5,6}, // Element 0,1
                // ...
                  },
                  { {"ExampleName","This is an example",3,4,5,6}, // Element 1,0
                // ... 
                  }
                };

这是您可以获得的最接近单行的,它是有效的C++(在所有变体中,鉴于board是一个聚合,因此board[10][10]也是一个聚合),但它可能很难阅读。

下一步是更接近您正在做的事情,即初始化每个元素(而不是数组),因为在 C++11 中,您可以使用与建议相同的初始化类型。在 C++03 中,您需要通过构造函数执行此操作(注意,这将更改类型的属性,并且您需要为数组创建默认构造函数):

 struct board {
    string name;
    string desc;
    int nval;
    int sval;
    int eval;
    int wval;
    board() {}  // [*]
    board( const char* name, const char* desc, int n, int s, int e, int w )
       : name(name), desc(desc), nval(n), sval(s), eval(e), wval(w)
    {}
 };
 board fields[10][10];  // [*] this requires the default constructor [*]
 fields[5][6] = board("ExampleName","This is an example",3,4,5,6);

或通过函数:

board create_board( const char* name, const char* desc, ... ) {
   board res = { name, desc, ... };
   return res;
}
请注意,在

这两种情况下,数组中的元素都是在数组初始化期间初始化的,然后在它们之上复制一个新对象。