// Program: vector_template.cpp // Author: ... #include #include namespace ifmp { class vector { public: typedef int* iterator; typedef const int* const_iterator; // default constructor // POST: *this is an empty vector vector (); // POST: *this is a vector of size s vector (const unsigned int s); // POST: *this is a vector of size s, elements initialized // with val vector (const unsigned int s, const int& val); // copy constructor vector (const vector& v); // assignment operator vector& operator= (const vector& v); // destructor ~vector (); // POST: *this was emptied void clear (); // POST: returns the size of the vector unsigned int size () const; // POST: returns the capacity of the vector unsigned int capacity () const; // POST: element with value val is appended to *this, // capacity of *this was increased if necessary void push_back (const int& val); // POST: returns an iterator pointing to the first element iterator begin (); const_iterator begin () const; // POST: returns a past-the-end iterator iterator end (); const_iterator end () const; // PRE: n < size() // POST: returns a reference to the n-th element int& operator[] (const unsigned int n); // PRE: n < size() // POST: returns a const reference to the n-th element const int& operator[] (const unsigned int n) const; private: iterator begin_; iterator end_; iterator end_of_memory_; // [some private member functions if you need some] }; // default constructor vector::vector () : begin_(0), end_(0), end_of_memory_(0) {} // constructor for size vector::vector (const unsigned int s) { // [your code ...] } // constructor for size and init value vector::vector (const unsigned int s, const int& val) { // [your code ...] } // copy constructor vector::vector (const vector& v) { // [your code ...] } // assignment operator vector& vector::operator= (const vector& v) { // [your code ...] } // destructor vector::~vector () { // [your code ...] } void vector::clear () { // [your code ...] } unsigned int vector::size () const { // [your code ...] } unsigned int vector::capacity () const { // [your code ...] } void vector::push_back (const int& val) { // [your code ...] } vector::iterator vector::begin () { // [your code ...] } vector::const_iterator vector::begin () const { // [your code ...] } vector::iterator vector::end () { // [your code ...] } vector::const_iterator vector::end () const { // [your code ...] } int& vector::operator[] (const unsigned int n) { // [your code ...] } const int& vector::operator[] (const unsigned int n) const { // [your code ...] } } // end namespace int main() { ifmp::vector vec; // Input std::cout << "Input numbers: "; int i; while (std::cin >> i) vec.push_back(i); // Test outputs std::cout << "Size = " << vec.size() << "\n"; std::cout << "size <= capacity ? " << (vec.size() < vec.capacity()) << "\n\n"; std::cout << "Output []: "; for (unsigned int i = 0; i < vec.size(); ++i) std::cout << vec[i] << " "; std::cout << "\n"; ifmp::vector vec2 = vec; std::cout << "Output itr: "; for (ifmp::vector::const_iterator itr = vec2.begin(); itr < vec2.end(); ++itr) std::cout << *itr << " "; std::cout << "\n"; std::cout << "Other Output: "; ifmp::vector vec3 (3,2); // construct and initialize for (unsigned int i = 0; i < vec2.size(); ++i) vec3.push_back(vec2[i]); for (unsigned int i = 0; i < vec3.size(); ++i) std::cout << vec3[i] << " "; std::cout << "\n"; return 0; }