With the help of the length variable, we can obtain the size of the array. Examples: int size = arr[]. length; // length can be used // for int[], double[], String[] // to know the length of the arrays.
We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);
int numberofelements = sizeof(array)/sizeof(array[0]); This is because sizeof(array) will return the sum of sizes of pointers corresponding to each string. sizeof(array[0]) will return the size of the pointer corresponding to the first string. Thus, sizeof(array)/sizeof(array[0]) returns the number of strings.
So I have an array of strings. Here's an example:
std::string array[] = {"Example", "Example2", "Example3"};
Is there any way I can find the number of elements in an array like the one above. I can't use this method:
int numberofelements = sizeof(array)/sizeof(array[0]);
This is because the size of the elements vary. Is there another way?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With