SubArray Processing
- For many problems we know the maximum capacity
- But not the number of elements in an array.
- Frequently problems involve a statement like "There will be no more that 200 records"
- Or you can put a bound on maximum size somehow.
- This is probably an academic exercise.
- In this case, you will need to store both
- The capacity or the maximum size of the array (a constant)
- The size or number of elements in the array (a variable)
- These will need to be passed to all functions that manipulate the array.
- Generally size is
- The number of actual elements in the array.
- Initially 0.
- It is also the next position in the array where something can be inserted.
- This means that the data is normally kept in the first 0..size-1 elements of the array.
- Inserting an element
- Usually at position size.
- Deleting an element
- Just move the element to be deleted to the end
- Fill in the hole created
- And decrement size.
- There is no "erase" an element, the memory is still allocated in the array.
- Wrapping the array and the size in a struct is convenient.
- Just remember to pass the struct by const reference instead of by value.
- Write a program to count the number of unique words in a file.