+ 2
how can i check the amount of assigned elements in a array
for example: int nums[5][5] = {{2,6,3},{0,3,5}}; how can i check how many element i have assigned?
2 Answers
+ 3
Dennis when do people use a array
+ 2
You don't, not as is at least.
You can assign each element not in use with a special value though, like INT_MIN and then simply iterate over the array and check how many are not INT_MIN.
But in the case where you can't use a special value like this then there is no way, not with arrays at least.
You should keep track of when you assign an element instead and keep it in a separate variable.
That's why you use std::vector most of the time, it has a handy size() member function that returns the number of elements in the array.