question about capacity memeber

closed account (EwCjE3v7)
Hello guys, I have this question from my book: "Why don`t list or array have a capacity member?

This is not homework, Im learning c++ myself.

Is it because the data isnt contiguous?
Arrays in C++ don't have a capacity (I assume you mean size) member because they are not an object, so they have no members! An array in C++ is essentially just a pointer. This is why you have you null-terminate your C-strings :)

std::list does have a size method. http://www.cplusplus.com/reference/list/list/size/

Apparently pre-C++11, the size of a list was computed on-the-fly, and now it's just stored as a member.

This has nothing to do with the contiguity (is that even a word?) of the data.
I would assume with list it is because the data can be stored in different locations and arrays have a fixed size so there is no need for capacity since it would be the same as size always. In say a vector the capacity (amount of allocated memory) could be 100 and only have 20 elements in it so a size of 20. IIRC if you reach the capacity then try to insert an element it will double the capacity so in that example go from 100 to 200. That way it will reallocate as little as possible. Though the capacity can only reach the max_size. You may also change the capacity manually with the reserve method.

@ResidentBiscuit
they are not an object
There is also std::array
Last edited on
closed account (EwCjE3v7)
@ResidentBiscuit Oh sorry I forgot to mention that they were talking about std::array, sorry about that. And that is why you thought I meant the size() memeber

@giblit Thank you for your explanation, clears everything up :)
Topic archived. No new replies allowed.