Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: BlackZeroX en 17 Junio 2011, 10:11 am



Título: [duda] implementando la clase reverse_iterator
Publicado por: BlackZeroX en 17 Junio 2011, 10:11 am
.
Estoy probando las plantillas que trae la STL y me viene a la mente usar la clase reverse_iterator.

La cuestión es como la uso, hasta ahora solo hice una cutre pero sin usar la plantilla reverse_iterator?

Código
  1.  
  2. #ifndef cbyte_H
  3. #define cbyte_H
  4.  
  5. #include <string.h>
  6. #include <iterator>
  7.  
  8. using namespace std;
  9.  
  10. #ifndef _BYTE_
  11. #define _BYTE_
  12. typedef unsigned char   byte;
  13. #endif  //#ifndef _BYTE_
  14.  
  15. class bytesiterator : public iterator<input_iterator_tag, byte> {
  16.    private:
  17.        const byte            *__p_vbyte;
  18.    public:
  19.        bytesiterator ( const byte *__pv_byte ) : __p_vbyte(__pv_byte) {  };
  20.        virtual ~bytesiterator() {};
  21.        bytesiterator   &operator++() {++this->__p_vbyte;return *this;}
  22.        bytesiterator   operator++(int) {bytesiterator tmp(*this); operator++(); return tmp;}
  23.        bytesiterator   &operator--() {--this->__p_vbyte;return *this;}
  24.        bytesiterator   operator--(int) {bytesiterator tmp(*this); operator--(); return tmp;}
  25.        bool operator   ==(const bytesiterator &__r_cbyte) {return this->__p_vbyte==__r_cbyte.__p_vbyte;}
  26.        bool operator   !=(const bytesiterator &__r_cbyte) {return this->__p_vbyte!=__r_cbyte.__p_vbyte;}
  27.        int operator    *() {return *this->__p_vbyte;}
  28. };
  29.  
  30. class bytesiteratorreverse : public iterator<input_iterator_tag, byte> {
  31.    private:
  32.        const byte            *__p_vbyte;
  33.    public:
  34.        bytesiteratorreverse ( const byte *__pv_byte ) : __p_vbyte(__pv_byte) {  };
  35.        virtual ~bytesiteratorreverse() {};
  36.        bytesiteratorreverse   &operator++() {--this->__p_vbyte;return *this;}
  37.        bytesiteratorreverse   operator++(int) {bytesiteratorreverse tmp(*this); operator++(); return tmp;}
  38.  
  39.        bytesiteratorreverse   &operator--() {++this->__p_vbyte;return *this;}
  40.        bytesiteratorreverse   operator--(int) {bytesiteratorreverse tmp(*this); operator--(); return tmp;}
  41.  
  42.        bool operator   ==(const bytesiteratorreverse &__r_cbyte) {return this->__p_vbyte==__r_cbyte.__p_vbyte;}
  43.        bool operator   !=(const bytesiteratorreverse &__r_cbyte) {return this->__p_vbyte!=__r_cbyte.__p_vbyte;}
  44.        int operator    *() {return *this->__p_vbyte;}
  45. };
  46.  
  47. #ifndef _bytecontainer_
  48. #define _bytecontainer_
  49.  
  50.  
  51. class bytes {
  52.    protected:
  53.        byte        *__p_vbytes;
  54.        size_t      _ln;
  55.  
  56.    public:
  57.        typedef bytesiterator               iterator;
  58.        typedef bytesiteratorreverse        reverse_iterator;
  59.  
  60.        bytes ( const byte* __p_vbytes , size_t _ln) {
  61.            this->__p_vbytes    = new byte[_ln];
  62.            this->_ln           = _ln;
  63.            memcpy( this->__p_vbytes , __p_vbytes , _ln );
  64.        }
  65.        bytes ( ) {
  66.            this->__p_vbytes    = NULL;
  67.            this->_ln           = 0;
  68.        }
  69.        bytes ( size_t _ln ) {
  70.            this->__p_vbytes    = new byte[_ln];
  71.            this->_ln           = _ln;
  72.            memset( this->__p_vbytes , 0 , _ln );
  73.        }
  74.        virtual ~bytes () { delete[] this->__p_vbytes; }
  75.  
  76.        size_t  leght() { return this->_ln; }
  77.        byte*   __p()   { return this->__p_vbytes; }
  78.        void clear()    { if ( this->__p_vbytes ) delete[] this->__p_vbytes; this->_ln = 0; }
  79.  
  80.        iterator begin() {
  81.            //bytesiterator _v_tmp( (const byte*)this->__p_vbytes );
  82.            iterator _v_tmp( (const byte*)this->__p_vbytes );
  83.            return _v_tmp;
  84.        }
  85.  
  86.        iterator end() {
  87.            //bytesiterator _v_tmp( (const byte*)(&this->__p_vbytes[this->_ln]) );
  88.            iterator _v_tmp( (const byte*)(&this->__p_vbytes[this->_ln]) );
  89.            return _v_tmp;
  90.        }
  91.  
  92.        reverse_iterator rbegin() {
  93.            //reverse_iterator _v_tmp( (const byte*)(&this->__p_vbytes[this->_ln-1]) );
  94.            reverse_iterator _v_tmp( (const byte*)(&this->__p_vbytes[this->_ln-1]) );
  95.            return _v_tmp;
  96.        }
  97.  
  98.        reverse_iterator rend() {
  99.            //reverse_iterator _v_tmp( (const byte*)(this->__p_vbytes-sizeof(byte)) );
  100.            reverse_iterator _v_tmp( (const byte*)(this->__p_vbytes-sizeof(byte)) );
  101.            return _v_tmp;
  102.        }
  103.  
  104.        bytes &operator+=( bytes &__p_cbyte) {
  105.            byte    *__p_vbytes = this->__p_vbytes;
  106.            size_t  _ln         = 0;
  107.            size_t  _pos        = 0;
  108.            if ( __p_cbyte.__p_vbytes!=NULL && __p_cbyte._ln>0 ) {
  109.                if ( this->_ln>0 && this->__p_vbytes!=NULL ) {
  110.                    _ln = this->_ln + __p_cbyte._ln;
  111.                    _pos = this->_ln;
  112.                } else {
  113.                    _ln = __p_cbyte._ln;
  114.                }
  115.                __p_vbytes = new byte[_ln];
  116.                memcpy( &__p_vbytes[_pos] , __p_cbyte.__p_vbytes , __p_cbyte._ln );
  117.  
  118.                if ( this->__p_vbytes!=NULL && this->_ln>0 ) {
  119.                    memcpy( __p_vbytes , this->__p_vbytes , this->_ln );
  120.                    delete[] this->__p_vbytes;
  121.                }
  122.            }
  123.            this->__p_vbytes    = __p_vbytes;
  124.            this->_ln           = _ln;
  125.            return *this;
  126.        }
  127.  
  128.        bytes &operator=( bytes &__p_cbyte ) {
  129.            byte    *__p_vbytes = NULL;
  130.            if ( this != &__p_cbyte ) {
  131.                this->_ln           = __p_cbyte._ln;
  132.                if ( this->_ln>0 && __p_cbyte.__p_vbytes != NULL ) {
  133.                    __p_vbytes          = new byte[this->_ln];
  134.                    memcpy( __p_vbytes , __p_cbyte.__p_vbytes , this->_ln );
  135.                } else {
  136.                    this->_ln           = 0;
  137.                }
  138.                delete[] this->__p_vbytes;
  139.                this->__p_vbytes = __p_vbytes;
  140.            }
  141.            return *this;
  142.        }
  143.  
  144.        operator byte*() {
  145.            return this->__p_vbytes;
  146.        }
  147.  
  148.        operator size_t() {
  149.            return this->_ln;
  150.        }
  151.  
  152.        operator long int() {
  153.            return (long int)this->_ln;
  154.        }
  155.  
  156.        operator unsigned long int() {
  157.            return (unsigned long int)this->_ln;
  158.        }
  159.  
  160.        bool operator ==( bytes &__pc_byte ) {
  161.            size_t  _szt_pos    = 0;
  162.  
  163.            if ( __pc_byte.__p_vbytes == this->__p_vbytes ) {
  164.                return true;
  165.            }
  166.            if ( (this->_ln == __pc_byte._ln) && (__p_vbytes != NULL) && (this->__p_vbytes!=NULL) ) {
  167.                while ( _szt_pos<this->_ln ) {
  168.                    if ( __pc_byte.__p_vbytes[_szt_pos] == this->__p_vbytes[_szt_pos] ) {
  169.                        _szt_pos++;
  170.                    } else {
  171.                        return false;
  172.                    }
  173.                };
  174.                return true;
  175.            }
  176.            return false;
  177.        }
  178. };
  179. #endif  //#ifndef _bytecontainer_
  180.  
  181. #endif // cbyte_H
  182.  
  183.  

P.D.: Revise esta liga http://www.cplusplus.com/reference/std/iterator/reverse_iterator/reverse_iterator/ pero cuando la implemento no me sirve ( con respecto a esta clase ).

Dulces Lunas!¡.