+ 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?

14th Jun 2019, 10:16 AM
Cat Sauce
Cat Sauce - avatar
2 Respostas
+ 3
Dennis when do people use a array
14th Jun 2019, 10:55 AM
Cat Sauce
Cat Sauce - avatar
+ 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.
14th Jun 2019, 10:20 AM
Dennis
Dennis - avatar