A function that directly obtains the length of an array is not available in C + +
One of the ways to get the array length is by using sizeof (array)/sizeof (Array[0]).
It is customary in C to define it as a macro when used, such as:
#define Get_array_len (array,len) {LEN = (sizeof (ARRAY)/sizeof (array[0));}
In C + +, you can use template technology to define a function, such as:
Template <class t>
int Getarraylen (t& array)
{
Return (sizeof (array)/sizeof (array[0));
}
This allows you to use this macro or this function to get the length of the array for some simple arrays.
C Language Usage:
#define Get_array_len (array,len) {LEN = (sizeof (ARRAY)/sizeof (array[0));}
Define a macro with parameters to store the length of the array in the variable len
int main ()
{
Char a[] = {' 1 ', ' 2 ', ' 3 ', ' 4 '};
int Len;
Get_array_len (A,len)
Call a predefined macro to get the length of array A and store it in the variable len
}
C + + Usage:
int main ()
{
Char a[] = {' 1 ', ' 2 ', ' 3 '};
cout << Getarraylen (a) << Endl;
}
Get the array length in C + +