From 0dd3866d124883afea685d3df5aa9beb9242b944 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 2 Feb 2024 13:07:31 -0500 Subject: [PATCH] remove const square bracket operator --- lectures/08_vector_implementation/main.cpp | 9 +- lectures/08_vector_implementation/vec.h | 184 ++++++++------------- lectures/08_vector_implementation/vec_v2.h | 77 --------- 3 files changed, 76 insertions(+), 194 deletions(-) delete mode 100644 lectures/08_vector_implementation/vec_v2.h diff --git a/lectures/08_vector_implementation/main.cpp b/lectures/08_vector_implementation/main.cpp index 046fb1b..6cad3ff 100644 --- a/lectures/08_vector_implementation/main.cpp +++ b/lectures/08_vector_implementation/main.cpp @@ -18,18 +18,18 @@ int main(){ teams.push_back("harvard"); // we can use a type alias defined inside a class even if there is no object of that class. The type alias becomes part of the class's scope and can be used anywhere in your code where the class's scope is visible. although no object of Vec is created in this code, the type alias size_type is still accessible because it is part of the class's scope. - for(Vec::size_type i = 0; i < teams.size(); i++){ + for(unsigned int i = 0; i < teams.size(); i++){ std::cout << teams[i] << std::endl; } std::cout<<"========="<::size_type i = 0; i < teams.size(); i++){ + for(unsigned int i = 0; i < teams.size(); i++){ std::cout << teams[i] << std::endl; } std::cout<<"========="<::size_type i = 0; i < teams.size(); i++){ + for(unsigned int i = 0; i < teams.size(); i++){ std::cout << teams[i] << std::endl; } std::cout<<"========="< w(v), w is a copy of the elements in v. - // we use the const keyword in front of a variable definition to indicate that the value of the variable cannot be changed after it is initialized. - const Vec w = v; + Vec w = v; for (unsigned int i = 0; i < v.size(); ++i){ std::cout << "w[" << i << "] is " << w[i] << " and v[" << i << "] is " << v[i] << std::endl; } diff --git a/lectures/08_vector_implementation/vec.h b/lectures/08_vector_implementation/vec.h index d78c5ac..3a15b17 100644 --- a/lectures/08_vector_implementation/vec.h +++ b/lectures/08_vector_implementation/vec.h @@ -1,119 +1,79 @@ -#ifndef Vec_h_ -#define Vec_h_ -// Simple implementation of the vector class, revised from Koenig and Moo. This -// class is implemented using a dynamically allocated array (of templated type T). -// We ensure that that m_size is always <= m_alloc and when a push_back or resize -// call would violate this condition, the data is copied to a larger array. +template +class Vec{ +public: + // default constructor + Vec(){ + m_data = new T[2]; + m_size = 0; + capacity = 2; + } -template class Vec { + // other constructor + Vec(int size, const T& val){ + m_data = new T[size]; + m_size = size; + capacity = size; + for(int i=0;icreate(); } - Vec(size_type n, const T& t = T()) { this->create(n, t); } - Vec(const Vec& v) { copy(v); } - Vec& operator=(const Vec& v); - ~Vec() { delete [] m_data; } + // destructor + ~Vec(){ + delete [] m_data; + } - // MEMBER FUNCTIONS AND OTHER OPERATORS - T& operator[] (size_type i) { return m_data[i]; } - const T& operator[] (size_type i) const { return m_data[i]; } - void push_back(const T& t); - void resize(size_type n, const T& fill_in_value = T()); - void clear() { delete [] m_data; create(); } - bool empty() const { return m_size == 0; } - size_type size() const { return m_size; } + // assignment operator + Vec& operator=(const Vec& other){ + if(this != &other){ + capacity = other.capacity; + m_size = other.m_size; + m_data = new T[m_size]; + for(unsigned int i=0;i& v); + // [] operator + T& operator[](int i){ + return m_data[i]; + } - // REPRESENTATION - T* m_data; // Pointer to first location in the allocated array - size_type m_size; // Number of elements stored in the vector - size_type m_alloc; // Number of array locations allocated, m_size <= m_alloc + unsigned int size(){ + return m_size; + } + + void push_back(const T& val){ + if(m_size >= capacity){ + capacity = capacity * 2; + // allocate memory for the new array and move content of m_data to the new array. + T* temp = new T[capacity]; + for(unsigned int i=0;i void Vec::create() { - m_data = NULL; - m_size = m_alloc = 0; // No memory allocated yet -} - -// Create a vector with size n, each location having the given value -template void Vec::create(size_type n, const T& val) { - m_data = new T[n]; - m_size = m_alloc = n; - for (size_type i = 0; i < m_size; i++) { - m_data[i] = val; - } -} - -// Assign one vector to another, avoiding duplicate copying. -template Vec& Vec::operator=(const Vec& v) { - if (this != &v) { - delete [] m_data; - this -> copy(v); - } - return *this; -} - -// Create the vector as a copy of the given vector. -template void Vec::copy(const Vec& v) { - - - - - - - - -} - -// Add an element to the end, resize if necesssary. -template void Vec::push_back(const T& val) { - if (m_size == m_alloc) { - // Allocate a larger array, and copy the old values - - - - - - - - - - - } - // Add the value at the last location and increment the bound - m_data[m_size] = val; - ++ m_size; -} - -// If n is less than or equal to the current size, just change the size. If n is -// greater than the current size, the new slots must be filled in with the given value. -// Re-allocation should occur only if necessary. push_back should not be used. -template void Vec::resize(size_type n, const T& fill_in_value) { - - - - - - - - - - - - - - - -} - -#endif diff --git a/lectures/08_vector_implementation/vec_v2.h b/lectures/08_vector_implementation/vec_v2.h deleted file mode 100644 index b4bb9bb..0000000 --- a/lectures/08_vector_implementation/vec_v2.h +++ /dev/null @@ -1,77 +0,0 @@ -template -class Vec{ -public: - typedef unsigned int size_type; - // default constructor - Vec(){ - m_data = new T[2]; - m_size = 0; - capacity = 2; - } - // other constructor - Vec(int size, const T& val){ - m_data = new T[size]; - m_size = size; - capacity = size; - for(int i=0;i& operator=(const Vec& other){ - if(this != &other){ - capacity = other.capacity; - m_size = other.m_size; - m_data = new T[m_size]; - for(unsigned int i=0;i= capacity){ - capacity = capacity * 2; - // allocate memory for the new array and move content of m_data to the new array. - T* temp = new T[capacity]; - for(unsigned int i=0;i