loops - custom list with for-each support c++ -
in start want apologize english. problem weird because i'm trying write own arraylist looks , works list in java , know it's reinvent wheel, fun , better understanding how works. every answer "use stl", "use vector", "use else don't create own list" not helpful. start, have own template arraylist few methods important me. arraylist works array use part of available space , if need more space create new bigger array , copy old elements (i beleive way works in java arraylist, tell me if i'm wrong). in array store pointers. , template save , remove known objects , if need read stored data, real problem appear when try write loop read, check , remove specified element list. can not using standard for(int i=0;i
- supporting "for each" on custom const native c++ container class
- supporting each loop in classes
- c++ each in on custom collections
- how make each loop function in c++ work custom class
- creating own iterators
- http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html
so can explain me should write in template support for-each functionality. in many examples appear begin() , end() functions no 1 explain why name, return type , why, , method should return. here template code, if wrong, please tell me. use code in other apps because me implementation more intuition vector (i long works java :))
template <class t> class arraylist { public: arraylist() { array = new t*[1000]; arraysize = 1000; n = 0; }; void add(t &arg) { //add new element @ end if (n == arraysize) { increase(); } array[n] = &arg; n++; }; void addat(t &arg, unsigned int pos) { //add new element @ specific position , override if (pos >= 0 && pos <= n) { if (pos == n) { add(arg); } else { array[pos] = &arg; } } else { throw "indexoutofboundexception"; } }; void addafter(t &arg, unsigned int pos) { //add new element between specific posittion , next element pos++; if (pos >= 0 && pos <= n) { if (n == arraysize) { increase(); } (unsigned int = n; > pos; i--) { array[i] = array[i - 1]; } array[pos] = &arg; n++; } else { throw "indexoutofboundexception"; } }; void addlist(arraylist &list) { //add 'list' @ end if (list.n > 0) { while (list.n + n > arraysize) { increase(); } (int = 0; < list.n; i++) { array[n] = list.array[i]; n++; } } }; void addlistafter(arraylist &list, unsigned int pos) { //put 'list' inside list, start 'pos' pos++; if (list.n > 0 && pos >= 0 && pos < n) { while (list.n + n > arraysize) { increase(); } int m = n - 1; while (m >= pos && m >= 0) { array[m + list.n] = array[m]; m--; } (int = 0; < list.n; i++) { array[pos + i] = list.array[i]; } n += list.n; } else { throw "indexoutofboundexception"; } }; void addlistafter(arraylist &list, t &arg) { //put 'list' inside list, start after t, if t not exist 'list' added @ end addlistafter(list, getindex(arg)); }; void remove(t &arg, bool all) { //remove selected element if all=true remove instance of object otherwise remove first if (all) { int copies = 0; (int index = 0; index < n; index++) { if (array[index] == &arg) { copies++; } else if (copies != 0) { array[index - copies] = array[index]; } } n -= copies; if (copies == 0) { throw "argumentnotfoundexception"; } while (arraysize - n >= 1000) { decrease(); } } else { remove(getindex(arg)); } }; void remove(unsigned int pos) { //remove element specific position if (pos >= 0 && pos < n) { (int = pos; < n - 1; i++) { array[i] = array[i + 1]; } n--; if (arraysize - n >= 1000) { decrease(); } } else { throw "indexoutofboundexception"; } }; void removecopy(t &arg) { //leaves 1 instance of object , remove other int copies = -1; (int index = 0; index < n; index++) { if (array[index] == &arg) { copies++; } else if (copies > 0) { array[index - copies] = array[index]; } } n -= copies; if (copies == -1) { n--; throw "argumentnotfoundexception"; } while (arraysize - n >= 1000) { decrease(); } }; void repair() { //leaves single instance of each object (int = 0; < n; i++) { removecopy(*array[i]); } }; void clear() { //remove object list (int = 0; < n; i++) { array[i] = null; } n = 0; }; t* get(unsigned int pos) { //return object on selected position if (pos >= 0 && pos < n) { return array[pos]; } else { throw "indexoutofboundexception"; } }; unsigned int getindex(t &arg) { //return position of selected object unsigned int index = 0; while (&arg != array[index] && index < n) { index++; } if (index == n) { throw "argumentnotfoundexception"; } return index; }; arraylist getsublist(unsigned int first, unsigned int last, bool deepcopy) { //return new list contains 'deep copy'/'copy reference' of elements (include) first (include) last. if deepcopy=true function return deep copy, otherwise return copy of reference. if (first < last&&first >= 0 && last < n) { arraylist<t> ret; (unsigned int = first; <= last; i++) { if (deepcopy) { ret.add(*new t(*array[i])); } else { ret.add(*array[i]); } } return ret; } throw "indexoutofboundexception"; }; unsigned int size() { //return size of list return n; }; bool isempty() { return n == 0; }; t *begin() { return &*array[0]; } t *end() { return &*array[n]; } private: unsigned int arraysize; //actual size of array unsigned int n; //number of elements in array t** array; void increase() { //increase size of array 1000 if (arraysize + 1000 <= long_max) { t** newarray = new t*[arraysize + 1000]; (unsigned int = 0; < arraysize; i++) { newarray[i] = array[i]; } delete[] array; array = newarray; arraysize += 1000; } else { throw "arraysizeoutofboundexception"; } }; void decrease() { //decrease size of array 1000 if (arraysize - 1000 > 0) { arraysize -= 1000; t** newarray = new t*[arraysize]; (unsigned int = 0; < arraysize; i++) { newarray[i] = array[i]; } delete[] array; array = newarray; } else { throw "arraysizeoutofboundexception"; } }; };
some of answers you've posted give explanations. begin , end return iterators container, begin referring first element , end position 1 item past last element. names, seem intuitive. believe iterator design chosen abstraction on pointers have minimal runtime cost.
i'm sure you've seen link in answers linked to, should refer page on range-based for-loops.
in case, seem confused elements of array vs. iterators pointing elements. with:
t **begin() { return &array[0]; } t **end() { return &array[n]; } your program work ranged-for. element type t*, not t.
Comments
Post a Comment