Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef VECTORITERATOR_H
- #define VECTORITERATOR_H
- #include <iterator>
- namespace mytemplate
- {
- template <class, class>
- class vector_iterator;
- template < class Type, class vector_class >
- class vector_const_iterator : public std::iterator<std::random_access_iterator_tag, Type>
- {
- private:
- Type* ptr;
- vector_const_iterator( Type* rhs ) : ptr( rhs ) {}
- public:
- friend vector_class;
- friend vector_iterator<Type, vector_class>;
- vector_const_iterator() : ptr( nullptr ) {}
- vector_const_iterator( const vector_const_iterator& rhs ) : ptr( rhs.ptr ) {}
- // Operatory : różne
- inline vector_const_iterator& operator=(const vector_const_iterator& rhs) { ptr = rhs.ptr; return *this; }
- inline vector_const_iterator& operator+=(ptrdiff_t rhs) { ptr += rhs; return *this; }
- inline vector_const_iterator& operator-=(ptrdiff_t rhs) { ptr -= rhs; return *this; }
- inline const Type& operator*() const { return *ptr; }
- inline const Type* operator->() const { return &(*ptr); }
- inline const Type& operator[](ptrdiff_t rhs) const { return ptr[rhs]; }
- // Operatory : arytmetyczne
- inline vector_const_iterator& operator++() { ++ptr; return *this; }
- inline vector_const_iterator& operator--() { --ptr; return *this; }
- inline vector_const_iterator operator++(int) { vector_const_iterator tmp(*this); ++ptr; return tmp; }
- inline vector_const_iterator operator--(int) { vector_const_iterator tmp(*this); --ptr; return tmp; }
- inline vector_const_iterator operator+(ptrdiff_t rhs) const { return ptr + rhs; }
- inline vector_const_iterator operator-(ptrdiff_t rhs) const { return ptr - rhs; }
- inline int operator-(const vector_const_iterator& rhs) const { return ptr - rhs.ptr; }
- friend inline vector_const_iterator operator+(ptrdiff_t lhs, vector_const_iterator& rhs) { return rhs + lhs; }
- // Operatory : porównania
- inline bool operator==(const vector_const_iterator& rhs) const { return ptr == rhs.ptr; }
- inline bool operator!=(const vector_const_iterator& rhs) const { return !(*this == rhs); }
- inline bool operator<(const vector_const_iterator& rhs) const { return ptr < rhs.ptr; }
- inline bool operator>(const vector_const_iterator& rhs) const { return ptr > rhs.ptr; }
- inline bool operator<=(const vector_const_iterator& rhs) const { return ptr <= rhs.ptr; }
- inline bool operator>=(const vector_const_iterator& rhs) const { return ptr >= rhs.ptr; }
- };
- template < class Type, class vector_class >
- class vector_iterator : public std::iterator<std::random_access_iterator_tag, Type>
- {
- private:
- Type* ptr;
- vector_iterator( Type* rhs ) : ptr( rhs ) {}
- public:
- friend vector_class;
- vector_iterator() : ptr( nullptr ) {}
- vector_iterator( const vector_iterator& rhs ) : ptr( rhs.ptr ) {}
- // Operatory : różne
- inline vector_iterator& operator=(const vector_iterator& rhs) { ptr = rhs.ptr; return *this; }
- inline vector_iterator& operator+=(ptrdiff_t rhs) { ptr += rhs; return *this; }
- inline vector_iterator& operator-=(ptrdiff_t rhs) { ptr -= rhs; return *this; }
- inline Type& operator*() const { return *ptr; }
- inline Type* operator->() const { return &(*ptr); }
- inline Type& operator[](ptrdiff_t rhs) const { return ptr[rhs]; }
- typedef vector_const_iterator<Type, vector_class> vec_const_iterator;
- inline operator vec_const_iterator() const { return vec_const_iterator( ptr ); }
- // Operatory : arytmetyczne
- inline vector_iterator& operator++() { ++ptr; return *this; }
- inline vector_iterator& operator--() { --ptr; return *this; }
- inline vector_iterator operator++(int) { vector_iterator tmp(*this); ++ptr; return tmp; }
- inline vector_iterator operator--(int) { vector_iterator tmp(*this); --ptr; return tmp; }
- inline vector_iterator operator+(ptrdiff_t rhs) const { return ptr + rhs; }
- inline vector_iterator operator-(ptrdiff_t rhs) const { return ptr - rhs; }
- inline int operator-(const vec_const_iterator& rhs) const { return ptr - rhs.ptr; }
- friend inline vector_iterator operator+(ptrdiff_t lhs, vector_iterator& rhs) { return rhs + lhs; }
- // Operatory : porównania
- inline bool operator==(const vec_const_iterator& rhs) const { return ptr == rhs.ptr; }
- inline bool operator!=(const vec_const_iterator& rhs) const { return !(*this == rhs); }
- inline bool operator<(const vec_const_iterator& rhs) const { return ptr < rhs.ptr; }
- inline bool operator>(const vec_const_iterator& rhs) const { return ptr > rhs.ptr; }
- inline bool operator<=(const vec_const_iterator& rhs) const { return ptr <= rhs.ptr; }
- inline bool operator>=(const vec_const_iterator& rhs) const { return ptr >= rhs.ptr; }
- };
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement