From 716d955a73be6267346e0c35bcf8d88d0be1c750 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 7 Feb 2025 13:11:17 -0500 Subject: [PATCH] adding the iterator implementation code --- lectures/10_linked_lists/vec.h | 145 ++++++++++++++++++++++++++ lectures/10_linked_lists/vec_main.cpp | 33 ++++++ 2 files changed, 178 insertions(+) create mode 100644 lectures/10_linked_lists/vec.h create mode 100644 lectures/10_linked_lists/vec_main.cpp diff --git a/lectures/10_linked_lists/vec.h b/lectures/10_linked_lists/vec.h new file mode 100644 index 0000000..5338cc5 --- /dev/null +++ b/lectures/10_linked_lists/vec.h @@ -0,0 +1,145 @@ +template +class vec { +public: + // default constructor + vec(){ + m_size = 0; + m_capacity = 2; + m_data = new T[m_capacity]; + // std::cout << "calling default constructor" << std::endl; + } + + // other constructor + vec(int number, const T& value){ + m_size = number; + m_capacity = m_size * 2; + m_data = new T[m_capacity]; + for(int i=0; i(){ + return ptr; + } + // pre-increment + iterator& operator++(){ + ++ptr; + return *this; + } + // post-increment + // special syntax here, we must write the int keyword here in the post-increment function. + iterator operator++(int){ + iterator temp = *this; + ++ptr; + return temp; + } + bool operator!=(const iterator& other){ + return (ptr != other.ptr); + } + }; + iterator begin(); + //iterator begin(){ + // we want to have an iterator pointing to the beginning of the vec container. + // return iterator(m_data); + //} + iterator end(){ + // we want to have an iterator pointing to the end. + return iterator(m_data + m_size); + } +private: + T* m_data; + int m_capacity; // whole capacity + int m_size; // current size +}; + +// below we demonstrate that member functions can also be written outside of the templated class definition: + +// why we need typename here: The compiler does not automatically assume that vec::iterator is a type. +// Instead, it first assumes that anything after :: is a member variable or function, not a type. +// To explicitly tell the compiler that vec::iterator is a type, we must use the typename keyword: +template +typename vec::iterator vec::begin(){ + return vec::iterator(m_data); +} + +template +int vec::size(){ + return m_size; +} diff --git a/lectures/10_linked_lists/vec_main.cpp b/lectures/10_linked_lists/vec_main.cpp new file mode 100644 index 0000000..4774e25 --- /dev/null +++ b/lectures/10_linked_lists/vec_main.cpp @@ -0,0 +1,33 @@ +#include +//#include +#include "vec.h" + +int main(){ + vec teams; + teams.push_back("rpi"); + teams.push_back("wpi"); + teams.push_back("yale"); + teams.push_back("brown"); + teams.push_back("cornell"); + teams.push_back("colgate"); + teams.push_back("miami"); + teams.push_back("colorado"); + teams.push_back("harvard"); + std::cout << "==== teams ==== " << std::endl; + int size = teams.size(); + for(int i=0; i v(4, 0.0); + v[0] = 13.1; v[2] = 3.14; + + vec::iterator itr = v.begin(); + std::cout << "the first element of v is: " << *itr << std::endl; + std::cout << "printing elements in v: " << std::endl; + while(itr != v.end()){ + std::cout << "print " << *itr << std::endl; + ++itr; + } +}