return number of elements STORED

I need help in getting the number of elements STORED in my array.
I have fixed array in size 5.

But my code is giving me size 5 and not the actual number of elements stored.

1
2
3
4
template<typename T, int k>
class List {
      T arr[k];
};


1
2
3
4
     size_t size() const {
        int s = sizeof(arr)/sizeof(arr[0]);
        return s;
    }
All variables always have something "stored" in them. So if you have an array of size 5, then you have 5 elements stored.

If you want to instead keep track of which elements have been assigned, you will have to do that on your own by keeping track of your own variable.
Could you give me a hint in how to keep track?
Well it would depend on how you are assigning elements.

Each time you assign an element that has not previously been assigned... just increment your counter by 1.
Alright. I got it. I added an int variable and use that in storing a new item.
Thanks.
Topic archived. No new replies allowed.