c++11 - using c++, how do I implement itetator to my own container class so i can use something like std::sort() -



c++11 - using c++, how do I implement itetator to my own container class so i can use something like std::sort() -

i'm trying larn how implement iterator functionality in own container class can utilize std::sort()

i've created own container class. how add together iterator functionality, begin(), end(), ...

template <class t> class myvector { public: myvector() : m_dataptr(null), m_nsize(0), m_nallocated(0) {} ~myvector() { delete m_dataptr; } size_t size() const { homecoming m_nsize; } void pushback(const t &data); private: t *m_dataptr; size_t m_nsize; size_t m_nallocated; }; //****************************** template <class t> void myvector<t>::pushback(const t &data) { if (m_nsize == m_nallocated) { m_nallocated = (m_nallocated+1) *2; t *tmp = new t[m_nallocated]; if (!tmp) return; // transfer info ptr tmp (size_t = 0; < m_nsize; i++) tmp[i] = m_dataptr[i]; // delete[] ptr , set tmp delete[] m_dataptr; m_dataptr = tmp; } m_dataptr[m_nsize++] = data; }

you may implement begin() , end() adding class definition.

t* begin() { homecoming m_dataptr; } t* end() { homecoming m_dataptr + m_nsize; }

your utilize of contiguous memory allows raw pointers function iterators.

c++ c++11 iterator containers

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -