STL Implementations and the const_iterator


Non-const and const Iterators

// non-const iterator
template <typename T>
class svector_iter
{
  public:
    typedef ptrdiff_t difference_type;
    svector_iter(T*);  // conversion constructor
    difference_type operator-(const svector_iter& rhs) const;
    svector_iter operator+(difference_type rhs)) const;
  private:
    T* elem_ptr_;  // A pointer to some element
};

// const iterator
template <typename T>
class const_svector_iter
{
  public:
    typedef ptrdiff_t difference_type;
    const_svector_iter(const T*);  // conversion constructor
    difference_type operator-(const svector_iter& rhs) const;
    const_svector_iter operator+(difference_type rhs) const;
  private:
    const T* elem_ptr_;  // A pointer to some element
};
The modified svector class:

// Forward declarations
template <typename T> class svector_iter;
template <typename T> class const_svector_iter;

template <typename T>
class svector
{
  public:
      // Typedefs for STL compatibility
    typedef T value_type;              // type in the container
    typedef T& reference;              // ref type in container
    typedef const T& const_reference;  // const ref type in container
    typedef svector_iter<T> iterator;  // iterator type for this container
    typedef ptrdiff_t difference_type; // type of diff between iterators
    typedef unsigned int size_type;    // type of sizes in container
    typedef const_svector_iter<T> const_iterator; // const iterator

    svector();                                   // default constructor
    void push_back(const_reference value);
    iterator insert(iterator it, const_reference value);
    iterator begin(void);
    void clear(void);
    ~svector();
  private:
    size_type size_;      // The number of elements in the array
    T* array_;            // The dynamically allocated array
    size_type capacity_;  // The allocated size of the array
    void grow(void);
};