/* vector.h */ #ifndef _vector_h #define _vector_h #include // includes the prototype for basic I/O #include // includes the prototype for "abort" #include // includes the prototype for "bcopy" #include // includes the prototype for cout, cin, etc. #include // include the prototype for "assert" template class Vector // Defines safe vectors of Elemtype. The vectors are safe in // that an attempt to access an index out of range will produce // an error message. They are dynamic, but can only grow at // the high end, and the indeces always go from 0 to size()-1. { public: // Effect: Create an empty vector. Vector (); // Requires: size >=0 // Effect: Create a vector of the given size, with indices 0 to size-1. // The elements of the vector will all be copies of e. Vector (int size, Elemtype e); // Effect: Create a new vector that is a copy of vector a. Vector (Vector &a); // Effect: Destroy the vector and all elements inside it. ~Vector (); // Requires: 0 <= index < size() // Modifies: this // Effect: If used in an expression (e.g., something on the right-hand // side of and assignment statement), returns the value of the // ith vector element and leaves the vector unchanged. If used // on the left-hand side of an assignment, replaces the ith // element of the vector with the right-hand side value. E.g., // a[i] = e; // or e = a[i] + a[j]; Elemtype & operator [] (int i); // Modifies: this // Effect: adds one new element, e, to the high end of the array. void addh (Elemtype e); // Requires: size() > 0 // Modifies: this // Effect: removes one new element, e, from the high end of the array // and returns it. Elemtype remh (); // Requires: Elemtype has an assignment operator // Effect: this object becomes a copy of a. Vector & operator = (Vector &a); // Effect: Returns the size of the vector. int size(); private: Elemtype* arrayElems; // the elements int arraySize; // the number of defined elements int spaceAllocated; // the number of elements allocated // Representation invariants: // 1) the size of arrayElems array == spaceAllocated // 2) 0 <= arraySize <= spaceAllocated }; // Requires: Elemtype has an == operator // Effect: returns true if a1 and a2 are copies of each other, // i.e., they have the same number of elements and for // all elements a1[i], a2[i], a1[i]==a2[i]. template bool operator == (Vector &a1, Vector &a2); // Requires: Elemtype has an output operator // Effect: print this object on the output stream os template ostream & operator << (ostream &os, Vector &v); #endif _vector_h