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 A[size] = e size ← size + 1
Item Added | New Array? | Size/Capacity (at start) | Copy Ops Needed | Average Copy Ops |
---|---|---|---|---|
1 | Y | 0/0 | 0 | 0 |
2 | Y | 1/1 | 1 item | 1/2 |
3 | Y | 2/2 | 2 items | (2+1)/3 = 3/3 |
4 | N | 3/4 | 0 | (2+1)/4 = 3/4 |
5 | Y | 4/4 | 4 | (4+2+1)/5 = 7/5 |
6 | N | 5/8 | 0 | (4+2+1)/6 = 7/6 |
7 | N | 6/8 | 0 | (4+2+1)/7 = 7/7 |
8 | N | 7/8 | 0 | (4+2+1)/8 = 7/8 |
9 | Y | 8/8 | 8 | (8+4+2+1)/9 = 15/9 |
10 | n | 9/16 | 0 | (8+4+2+1)/10 = 15/10 |
11 | n | 10/16 | 0 | (8+4+2+1)/11 = 15/11 |
12 | n | 11/16 | 0 | (8+4+2+1)/12 = 15/12 |
13 | n | 12/16 | 0 | (8+4+2+1)/13 = 15/13 |
14 | n | 13/16 | 0 | (8+4+2+1)/14 = 15/14 |
15 | n | 14/16 | 0 | (8+4+2+1)/15 = 15/15 |
16 | n | 15/16 | 0 | (8+4+2+1)/16 = 15/16 |
pop_back
.