如何在 C++ 中初始化一个空的整数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27725928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to initialize an empty integer array in c++
提问by nikhil
how to put NULL or empty value to an integer array?
如何将 NULL 或空值放入整数数组?
struct book{
char name;
char aut;
int det[10];
};
book bk[40];
here i want every book data type to have there det arrays have value zero for every det[] member.
在这里,我希望每个书籍数据类型都有 det 数组的每个 det[] 成员的值为零。
回答by Vlad from Moscow
I think you mean how to zero-initialize an integer array.:)
我想你的意思是如何对整数数组进行零初始化。:)
You can define the array like
您可以像这样定义数组
int det[10] = {};
or
或者
int det[10] = { 0 };
You may use such a declaration in a structure.
您可以在结构中使用这样的声明。
If the array is already defined then you can use memset. For example
如果已经定义了数组,则可以使用 memset。例如
#include <cstring>
//...
std::memset( det, 0, sizeof( det ) );
回答by Keith Thompson
You don't.
你没有。
Your array det
consists of 10 elements, each of which is an object of type int
. The length of the array cannot be changed after its creation, and each element has someint
value (or contains undefined garbage if it hasn't been initialized).
您的数组det
由 10 个元素组成,每个元素都是一个类型为 的对象int
。数组的长度在创建后不能改变,每个元素都有 一些int
值(如果没有初始化,则包含未定义的垃圾)。
For pointer types, there's a special "null pointer" value that's doesn't point to anything and is distinct from all pointer values that are the address of something.
对于指针类型,有一个特殊的“空指针”值,它不指向任何东西,并且与作为某物地址的所有指针值不同。
There is no corresponding "null" or "empty" value for type int
. 0
is a perfectly valid int
value.
type 没有对应的“空”或“空”值int
。0
是一个完全有效的int
值。
If you want to keep track of which elements are valid and which or not, you'll have to use some other method. For example, you might arbitrarily pick some value to denote an invalid entry. You can use 0
for this purpose if you like, but then you won't be able to use 0
as a valid entry. Or you can use INT_MIN
, the most negative int
value, which is less likely to conflict with a meaningful value. But then you have to write your code so it consistently pays attention to the special value you've chosen.
如果您想跟踪哪些元素有效以及哪些元素无效,则必须使用其他方法。例如,您可以任意选择一些值来表示无效条目。0
如果您愿意,您可以将其用于此目的,但是您将无法0
用作有效条目。或者您可以使用INT_MIN
最负值int
,它不太可能与有意义的值发生冲突。但是随后您必须编写代码,以便它始终关注您选择的特殊值。
Or you can use a different data structure, such as a std::vector <int>
, that lets you change the length. Which data structure you should use depends on what you're trying to accomplish, which is not clear from your question.
或者您可以使用不同的数据结构,例如 a std::vector <int>
,它可以让您更改长度。您应该使用哪种数据结构取决于您要完成的任务,这从您的问题中不清楚。
回答by Captain Obvlious
C++11 changed the semantics of initializing an array during construction of an object. By including them in the ctor initializer list and initializing them with empty braces or parenthesis the elements in the array will be default initialized.
C++11 改变了在构造对象期间初始化数组的语义。通过将它们包含在 ctor 初始化列表中并用空大括号或括号初始化它们,数组中的元素将被默认初始化。
struct foo
{
int x[100];
foo() : x{} {}
};
In this case each element in foo:x
will be initialized to zero.
在这种情况下,每个元素都foo:x
将被初始化为零。
If you are not using C++11 you can initialize the elements of the array in the constructor body with std::memset
.
如果您不使用 C++11,您可以使用std::memset
.
struct foo
{
int x[100];
foo()
{
std::memset(x, 0, sizeof(x));
}
};
回答by user2649644
For an int array, you have to initialize it to 0, not null.
对于 int 数组,您必须将其初始化为 0,而不是 null。
int det[10] = { 0 };
For string arrays, you initialize the elements to null, but not for an int.
对于字符串数组,您将元素初始化为 null,而不是 int。