size
returns the size of the array
capacity
returns the capacity of the array.
append
, push
or push_back
which inserts an element on the end of the array.
push_back(ItemT e) if size ≥ capacity capacity ← capacty * 2 allocate newA copy data from A to newA delete A let A = newA A[size] = e size ← size + 1
Item Added | Size/Capacity (at start) | Need new array? | Copy Ops Needed | Average Copy Ops |
---|---|---|---|---|
1 | 0/0 | Y | 0 | 0 |
2 | 1/1 | Y | 1 item | 1/2 |
3 | 2/2 | Y | 2 items | (2+1)/3 = 3/3 |
4 | 3/4 | N | 0 | (2+1)/4 = 3/4 |
5 | 4/4 | Y | 4 | (4+2+1)/5 = 7/5 |
6 | 5/8 | N | 0 | (4+2+1)/6 = 7/6 |
7 | 6/8 | N | 0 | (4+2+1)/7 = 7/7 |
8 | 7/8 | N | 0 | (4+2+1)/8 = 7/8 |
9 | 8/8 | Y | 8 | (8+4+2+1)/9 = 15/9 |
10 | 9/16 | N | 0 | (8+4+2+1)/10 = 15/10 |
11 | 10/16 | N | 0 | (8+4+2+1)/11 = 15/11 |
12 | 11/16 | N | 0 | (8+4+2+1)/12 = 15/12 |
13 | 12/16 | N | 0 | (8+4+2+1)/13 = 15/13 |
14 | 13/16 | N | 0 | (8+4+2+1)/14 = 15/14 |
15 | 14/16 | N | 0 | (8+4+2+1)/15 = 15/15 |
16 | 15/16 | N | 0 | (8+4+2+1)/16 = 15/16 |
pop_back
.