Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the size of an array of strings in C++

Tags:

People also ask

How do you find the size of an array of strings?

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.

How do you find the size of an array in C?

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]);

How do I get the length of an array of strings in C++?

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?