secure Iterator
Mar 20, 2017 at 5:22pm UTC
hey i wrote this Vector class and the classes const_iterator and Iterator. now i have to secure them so that .end() doesnt be dereferented or incredimented with for instance the ++ operator... and i have no idea how to do so becouse .end() is a Vector methode and no Iterator methode
i was told that i have to write a new constructor with a pointer to Vector but a pointer will not have the method .end() either...
sorry for my bad english btw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
class Vector{
public :
using value_type= double ;
using size_type= size_t;
using difference_type= ptrdiff_t;
using reference = double &;
using const_reference= const double &;
using pointer = double *;
using const_pointer= const double *;
using iterator = double *;
using const_iterator= const double *;
private :
size_t sz;
size_t max_sz;
double * values=nullptr ;
class const_Iterator{
public :
using value_type = double ;
using difference_type = ptrdiff_t;
using reference = double &;
using pointer = double *;
using iterator_category = std::forward_iterator_tag;
private :
double * ptr;
size_t cnt;
public :
const_Iterator(double * p){
ptr=p;
cnt=0;
}
const_Iterator& operator ++ () {
ptr++;
cnt = 0;
return *this ;
}
bool operator ==(const const_Iterator& rop)const {
return this ->ptr == rop.ptr;
}
bool operator !=(const const_Iterator& rop)const {
return this ->ptr != rop.ptr;
}
const double operator * () const {
return *ptr;
}
friend Vector::difference_type operator -(const Vector::const_Iterator& lop,const Vector::const_Iterator& rop) {
return lop.ptr-rop.ptr;
}
};
class Iterator{
public :
using value_type = double ;
using difference_type = ptrdiff_t;
using reference = double &;
using pointer = double *;
using iterator_category = std::forward_iterator_tag;
private :
double * ptr;
size_t cnt;
public :
Iterator(double * p){
ptr=p;
cnt=0;
}
Iterator& operator ++() {
ptr++;
cnt = 0;
return *this ;
}
Iterator operator ++(int ){
Iterator a(ptr);
ptr++;
return a;
}
bool operator ==( Iterator& rop) {
return this ->ptr != rop.ptr;
}
bool operator !=(const Iterator& rop) {
return this ->ptr != rop.ptr;
}
double & operator *() {
return *ptr;
}
operator const_Iterator() const {
return const_Iterator(ptr);
};
};
const_Iterator end() const {return const_Iterator(values+sz);}
const_Iterator begin() const {return const_Iterator(values);}
Iterator begin() { return values; }
Iterator end() { return values + sz; }
size_t min_sz = 5;
Vector();
Vector(size_t);
Vector(const Vector&);
Vector (initializer_list<double > );
void push_back(double );
void reserve(size_t);
void pop_back();
bool empty();
void clear();
Vector& operator =(const Vector&);
const double & operator [] (size_t) const ;
double & operator [] (size_t) ;
void fit_to_shrink();
size_t size()const {return sz;}
ostream& print(ostream&) const ;
};
Topic archived. No new replies allowed.