Is there any way to find the number of elements in an array in VC++ without using for loop.
Let us say array has 16 elements;
int a[16];
a={1,2,3,4,5,6,7};
Now I if I want to get the length to be equal to 7 & NOT 16 (I mean I want to know the number of elements stored in this array instead of getting the number of spaces in the array). Is there any way similiar to the length() functions we have in C#?
I am using VC++ on VS2008.
Thanks in advance.
For built-in arrays, the only way to do this is for you to keep track of this “length” yourself. There’s no automatic way of doing it.
A better way, however, is to use
std::vector<int>. It automatically keeps track of both the current number of elements (the “size”) and the number of elements that can be accommodated without re-allocating the array (the “capacity”).