Project 2: Vector Class Template Container

Due 10/06/2023

Educational Objectives: Understanding generic programming and information hiding by developing generic containers; getting familiar with the concept of class template and its usage; using iterators, namespace, and operator overloading; analyzing algorithm complexity. 

Statement of Work: Implement a vector class template Vector and its associated iterator. Analyze the complexity of member functions of the developed Vector.

Requirements:

  1. A header file Vector.h is provided, which contains the interface of the vector class template Vector. In addition to data members and member functions of Vector, both iterator and const_iterator are also defined for Vector. The header file also contains a number of global non-class function templates (overloaded operators). You cannot change anything in the Vector.h file.

  2. A driver program test_vector.cpp is also provided. It is used to test your implementation of the vector class template for different data types (it tests Vector<int> and Vector<string>). Similarly, you cannot change anything in the test_vector.cpp file. Note that additional tests will be performed to check your implementation of the Vector class template.

  3. You need to implement the member functions of the vector class template Vector in a file named Vector.hpp. Note that, Vector.hpp has been included in the header file Vector.h (towards the end of the file). As we have discussed in class, you should not try to compile Vector.hpp (or Vector.h). Instead, you should compile the driver program test_driver.cpp to obtain the executable program. You need to implement all the member functions of Vector class template and non-class global overloaded functions operator==(), operator!=(), and operator<<() included in Vector.h.  Vector has three member variables, theSize, theCapacity, and array. Member variable theSize records the number of elements currently stored in the vector; theCapacity indicates the maximum number of elements the vector can hold without requesting new memory allocation; and array is a pointer of type T (the memory should be dynamically allocated).  The member variable array is used to store elements of the vector. The design of the Vector container closely follows the vector container included in C++/STL, which is also similar to the one presented in the textbook. It is OK for you to adapt the code provided in the textbook. However, you need to note that there are minor differences between the design of the Vector class in this project and the one given given in the textbook. In particular, whenever you need to insert a new element into the vector and the vector is full, you need to double the capacity. If the current capacity is zero, change the new capacity to 1. We describe the requirements of each function in the following.

         Member functions of Vector class template: 

           Non-class global functions 

  1. Write a makefile for your project and name your executable as proj2.x. Your program must be able to compile and run on the linprog machines.

  2. Analyze the worst-case run-time complexity of the member functions empty(), erase(iterator itr), and pop_back() of the Vector. Give the complexity in the form of Big-O. Your analysis must be clearly understandable by others. Name the file containing the complexity analysis as "analysis.txt".

Downloads

Click here to download the tar file, which contains the following files: Vector.h, test_vector.cpp, and proj2.x. The sample executable program proj2.x was compiled on a linprog machine from test_vector.cpp.

Note: The first person to find a programming error in our program will get a bonus point! (There is no known error in the program.)

Deliverables

Turn in files makefile, Vector.hpp, and analysis.txt in a single tar file via the Canvas. If you cannot finish all functions, you must also submit a modified test_vector.cpp (this file must be the original provided test_vector.cpp with some lines commented out) that would allow the executable to be produced to avoid your program being treated as having compiling errors.

Grading

Hints