In this project, you will extend and build on the IntVector data type that we built in class. You should refer to that code during this project (either the paper handout or the code from the class website).
You will create a CoolVector data type that has all the same capabilities as an IntVector but more as well:
CoolVector.cpp
file to fill in all the functions that are blank right now.main.cpp
which is a “driver” program that demonstrates the functionality of the CoolVector class.The driver program maintains two CoolVectors, called a
and b
, and allows the user to manipulate them in various ways by reading commands from a text file. Each command your driver program must handle has a corresponding C++ member function that you must call, so the driver program does little more than translate commands from the text file into function calls on CoolVectors a and/or b.
The commands your program must handle are: (in the descriptions below, letter
will either be a
or b
in the text files your program reads)
get letter position
: This command retrieves and prints the integer at a certain position in either CoolVector a
or b
by calling the get()
member function. You may assume 0 <= position < size()
.
Example: get a 2
would call a.get(2)
to retrieve and print a[2]
.
set letter position value
: This command sets the integer at a certain position in CoolVector a
or b
by calling the set()
member function. You may assume 0 <= position < size()
.
Example: set a 2 10
would call a.set(2, 10)
to set a[2]
equal to 10.
append letter value
: This command appends an integer to the end of CoolVector a
or b
by calling the append()
member function.
Example: append a 10
would call a.append(10)
to append the integer 10 to the end of CoolVector a
.
append letter1 letter2
: This command appends one CoolVector to another by calling the append()
member function. The second CoolVector is appended to the end of the first, leaving the second unchanged. Self-appending is possible (that is, letter1
may be equal to letter2
).
Example: append a b
would call a.append(b)
to append the contents of CoolVector b
to the end of CoolVector a
. CoolVector b
would not change.
prepend letter value
or prepend letter1 letter2
: Similar to the append
commands.
insert letter position value
: This command inserts a new integer into a CoolVector by calling the insert()
member function. You may assume 0 <= position <= size()
. Notice that position
may equal size()
, and in that case the new integer is inserted at the end of the CoolVector.
Example: insert a 4 6
would call a.insert(4, 6)
to insert the integer 6 at position 4
in CoolVector a
. If a
had any items at positions 4 or more, those would be slid
to the right to make room.
insert letter1 position letter2
: This command inserts all the integers in a CoolVector into another CoolVector by calling the insert()
member function. You may assume 0 <= position <= size()
. Notice that position
may equal size()
, and in that case the new items are inserted at the end of the CoolVector.
Example: insert a 4 b
would call a.insert(4, b)
to insert all the items in CoolVector b
into CoolVector a
, starting at position 4. If a
had any items at positions 4 or more, those would be slid to the right to make room.
clear letter
: This command removes all the integers in a CoolVector by calling the clear()
member function.
Example: clear a
would call a.clear()
.
remove letter position
: This command removes an item from a CoolVector at the given position. You may assume 0 <= position < size()
. Capacity does not change.
Example: remove a 3
would call a.remove(3)
to remove the integer at position 3, and all
items at positions 4 or more would be slid to the left.
remove letter start end
. This command removes the items from the CoolVector at
positions start, start+1, start+2, …end-1. Position end is not removed. Capacity does not change.
Example: remove a 3 5
would remove positions 3 and 4, but not 5.
slice letter1 start end letter2
. Slices the first CoolVector from start to end and puts the result in the second CoolVector. In other words, copies items start, start+1, start+2, …, end-1 from the first CoolVector into the second CoolVector. You may assume start <= end
. If start == end
, then a zero-length CoolVector is created.
Example : slice a 2 4 b
would call b = a.slice(2, 4)
.
Mostly all you have to add to main.cpp
is more if
/else
statements to handle the other commands. append
is already done for you; you can use it as a guide.
All operations should be as efficient as reasonably possible, especially in terms of big-O. Watch out for situations where you make extra loops over the CoolVector that you don’t need.
The only place you should be using regular C++ vectors (from #include <vector>
) is in the split()
function in main()
. Otherwise, everything should be done with CoolVectors.
For adding single elements at a time to a CoolVector, follow the same rules as we did for IntVector (expanding by units of SIZE_INCREMENT
).
For adding multiple elements at a time (inserting/appending/prepending another CoolVector), expand (if needed) just enough to fit the new items in, no more, no less.
When removing elements from a CoolVector, do not reduce the capacity.
If you need assistance implementing the copy constructor, destructor, operator=, or operator<<, see the IntVector code, or
try these references:
Overloading operator<<
Overloading operator=
Copy constructor