在数组中一次分配整个结构
Assign a whole struct at once in an array
我正在学习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;
}
请注意,在这两种情况下,数组中的元素都是在数组初始化期间初始化的,然后在它们之上复制一个新对象。
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 在C++中一次将矢量值写入多个文件
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么 zlib 放气初始化调用一次不起作用?
- 在一次迭代中从 txt 文件中读取多行
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 映射和设置总是一次分配一个项目吗
- 分配两个调用cudaMalloc一次的数组
- 这将是在将函数分配给变量或一次又一次地调用函数之间使用函数结果的最佳方式
- 如何将while循环的每一次迭代都分配给线程
- CUDA:可以在多内核中使用一次分配矩阵
- 通过调用一次新运算符大容量分配对象
- 在数组中一次分配整个结构
- 在堆栈上分配一个小对象比在堆上创建它(一次)更有效吗
- 我可以使用static关键字只分配一次新内存吗
- 在循环中只为函数内的变量分配一次内存
- 只为静态变量分配一次内存