I read to get the length of array in C++, you do this:
int arr[17];
int arrSize = sizeof(arr) / sizeof(int);
I tried to do the same for a string:
where I have
string * arr;
arr = new (nothrow) string [213561];
And then I do
arr[k] = "stuff";
where I loop through each index and put “stuff” in it.
Now I want the size of the array which should be 213561, what’s the correct way to do it and why is it so complex in C++?
The correct way of doing this in C++ is to use a
vector. That way you can either specify a size up-front, or resize it as you go.Specifying size up-front:
Expanding the vector as you go:
Either way, the size of the array is found with: