According to c++ reference, vector's .capacity() function returns the "size of allocated storage capacity".
Doesn't this mean that a vector {1} should have capacity of sizeof(1) like an array? If so, then why does this code:
int main() {
vector<int> v = {1};
cout << v.capacity() << endl;
return 0;
}
produce 1 rather than 4(sizeof(v[0]))?
I just started learning c++, so forgive me if I sound foolish.
sizeof
,vector::capacity
returns the number of elements, not the number of bytes. – Nathan Piersonstd::vector::capacity
returns the number of elements the container has space for, not the number of allocated bytes. – bnaecker