This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
Iterators for basic_string/vector
- To: Benjamin Kosnik <bkoz@cygnus.com>
- Subject: Iterators for basic_string/vector
- From: Ryszard Kabatek <rysio@rumcajs.chemie.uni-halle.de>
- Date: Mon, 4 Jan 1999 12:53:46 +0100 (CET)
- cc: libstdc++-v3@cygnus.com
- Reply-To: kabatek@Chemie.UNI-Halle.DE
There are still no iterator classes for basic_string and vector
in the latest snapshot.
My proposal (see below) is to use one implementation for both.
Usage:
template<typename _Tp> class basic_string/vector {
public:
class iterator : public _PtrIterator<_Tp> {
friend class basic_string/vector;
iterator(pointer __p)
: _PtrIterator<_Tp>(__p) {}
public:
iterator() {}
};
class const_iterator : public _PtrConstIterator<_Tp> {
friend class basic_string/vector;
const_iterator(pointer __p)
: _PtrConstIterator<_Tp>(__p) {}
public:
const_iterator() {}
const_iterator(const iterator<_Tp>& __i)
: _PtrConstIterator<_Tp>(__i) {}
};
// ...
}; // basic_string/vector
If You agree I could adapt this code for basic_string and vector.
Ryszard Kabatek
Martin-Luther University Halle-Wittenberg, Department of Physical
Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 Fax. +49 3461 46 2129
# ifndef _CPP_BITS_PTR_ITERATOR_H
# define _CPP_BITS_PTR_ITERATOR_H
# include <iterator>
template<typename _Tp>
class _PtrIteratorBase : public iterator<random_access_iterator_tag, _Tp>
{
typedef _PtrIteratorBase _Self;
public:
typedef const reference const_reference;
typedef const pointer const_pointer;
public:
bool
operator==(const _Self& rhs) const {return _M_data == rhs._M_data;}
bool
operator!=(const _Self& rhs) const {return _M_data != rhs._M_data;}
bool
operator< (const _Self& rhs) const {return _M_data < rhs._M_data;}
bool
operator> (const _Self& rhs) const {return _M_data > rhs._M_data;}
bool
operator<=(const _Self& rhs) const {return _M_data <= rhs._M_data;}
bool
operator>=(const _Self& rhs) const {return _M_data >= rhs._M_data;}
difference_type
operator-(const _Self& rhs) const {return _M_data - rhs._M_data;}
protected:
explicit _PtrIteratorBase(pointer __p = 0)
: _M_data(__p) {}
pointer&
_M_address() const {return _M_data;}
protected:
pointer _M_data;
};
template<typename _Tp>
class _PtrIterator : public _PtrIteratorBase<_Tp> {
typedef _PtrIterator _Self;
protected:
explicit _PtrIterator(pointer __p = 0)
: _PtrIteratorBase<_Tp>(__p) {}
public:
reference
operator*() const {return *_M_data;}
pointer
operator->() const {return _M_data;}
_Self&
operator++() {++_M_data; return *this;}
_Self
operator++(int) {return _PtrIterator(_M_data++);}
_Self&
operator--() {--_M_data; return *this;}
_Self
operator--(int) {_PtrIterator(_M_data--);}
_Self&
operator+=(difference_type __n) {_M_data += __n; return *this;}
_Self&
operator-=(difference_type __n) {_M_data -= __n; return *this;}
reference
operator[](difference_type __pos) const {return *(_M_data + __pos);}
};
template<typename _Tp>
class _PtrConstIterator : public _PtrIteratorBase<_Tp> {
typedef _PtrConstIterator _Self;
protected:
explicit _PtrConstIterator(pointer __p = 0)
: _PtrIteratorBase<_Tp>(__p) {}
_PtrConstIterator(const _PtrIterator<_Tp>& __rhs)
: _PtrIteratorBase<_Tp>(__rhs) {}
public:
const_reference
operator*() const {return *_M_data;}
const_pointer
operator->() const {return _M_data;}
_Self&
operator++() {++_M_data; return *this;}
_Self
operator++(int) {return _PtrConstIterator(_M_data++);}
_Self&
operator--() {--_M_data; return *this;}
_Self
operator--(int) {return _PtrConstIterator(_M_data--);}
_Self&
operator+=(difference_type __n) {_M_data += __n; return *this;}
_Self&
operator-=(difference_type __n) {_M_data -= __n; return *this;}
const_reference
operator[](difference_type __pos) const {return *(_M_data + __pos);}
};
# endif // _CPP_BITS_PTR_ITERATOR_H