Record about the automatic initialization of C++ integer array to zero
Automatically initialize to zero
Given an element value during declaration, other elements in the array are initialized to zero at this time.
int a[5] = {
0};
Take a look at the results:
Manually initialize the array to zero
method one
int a[5];
for(int i=0;i<sizeof(a)/4;i++)
a[i] = 0;
Way two
First introduce the header file cstring
int a[5];
memset(a,0,sizeof(a));
Note that the memset function here, because it initializes the memory block by bytes, it cannot initialize the integer array to a number other than 0 or -1, and the third parameter should be the integer array size multiplied by the number of bytes occupied by the integer 4 .
Recommended by other articles in this blog
Linear time selection for algorithm design and analysis
Divide and conquer strategy exercises for algorithm design and analysis (part 2)
Divide and conquer strategy exercises for algorithm design and analysis (on)
Divide and conquer strategy for algorithm design and analysis
Algorithm design and analysis of recursive algorithm exercises (part 2)
Algorithm design and analysis of recursive algorithm exercises (on)