October 24th, 2011, 11:23 am
I have created my own multi-dimensional array class. I then wanted to make it work with STL libraries like <algorithm> and <numeric>. It seems all I needed to to was make sure there is functions pointing to start and end of data that is stored (i.e. begin() and end()).1. Do I need to worry about creating STL typedef to create STL compliant classes. What is their benifit?2. I think I have created a bi-directional iterator? Why does STL work with and iterator classes. Is this something I should be doing i.e. what are the benifits? There might some errors on below as I did this from memorytemplate<typename _T, size_t _N>class Array{public: // STL typedefs typedef _T* pointer; typedef _T* iterator; ...private: // member variables pointer _data; size_t _length; size_t _extents[_N]; ...public: // iterators iterator begin() { return _data; } iterator end() { return _data + _length; } ...};#include <numeric>int main(){ Array<int,3> = A(1,2,3); // declares 3-dimensional array with dimensions 1x2x3 // fill array... int sum = std::accumulate(A.begin(),A.end(),0) // forward iteration for(Array<int,3>::iterator it=A.begin() ; it!=A.begin(), it++) { ... } // backward iteration for(Array<int,3>::iterator it=A.end()-1 ; it!=A.begin(), it--) { ... }}
Last edited by
MaxCohen on October 23rd, 2011, 10:00 pm, edited 1 time in total.