// ...
// an array A of type T, capacity N and size S where S is always smaller than N.
const std::size_t N = 100 ; // capacity
int /* type T */ A[N] ; // array
std::size_t S = 0 ; // logical size (initially zero)
// ...
// append a value to the array
constint value = 46 ;
if( S < N ) // if there is space in the array (S is smaller than N)
{
A[S] = value ; // place the value at the end
++S ; // and increment the logical size
}
// ...
sizeof(ar) ; // size of the array in bytes ie. capacity * size of each element ie. 100 * sizeof(int)
sizeof(ar) / sizeof( ar[0] ) ; // number of elements in the array ie. capacity
Yes, except the size and capacity are managed differently in the case of an ordinary array. A C-string might be declaclared as xyz[100] but only 10 elements followed by the null terminator apply in a particular case. So capacity = 100, size =10.
Vectors manage size and capacity 'automatically'.
http://www.cplusplus.com/reference/vector/vector/capacity/ might be of interest.
You have a jar. The jar has a fixed size. You have put 3 coins in it. If you want to take a coin, then you must choose only from those three coins, i.e. the logical size is 3, even though the jar has capacity to hold more coins.
There is a difference between vector and array though.
1 2 3 4 5 6 7 8
const size_t N = 7;
Foo bar[ N ];
bar[ 0 ] = Foo( "Hello", 42 );
size_t S = 1;
std::vector<Foo> gaz;
gaz.reserve( N );
gaz.emplace_back( "Hello", 42 );
Both bar and gaz have (currently) capacity to hold N Foos.
Both bar and gaz have one Foo. (logical size == 1)