pointer & iterator c++ -


i must project. have sparse matrix, 3 array:

  1. val
  2. pr rows position
  3. pc columns position

now i've problem iterator. must return pointer struct contain 3 thing:

  1. value
  2. row
  3. columns
class const_iterator; // forward declaration  class iterator {     friend class const_iterator;       friend class sparsematrix;  public:      typedef std::forward_iterator_tag iterator_category; typedef t value_type; typedef ptrdiff_t difference_type; typedef t* pointer; typedef t& reference;      iterator() : ptr(0) {}      iterator(const iterator &other) : ptr(other.ptr) {}      iterator& operator=(const iterator &other) {         ptr = other.ptr;         return *this;     }     ~iterator() {}        /**     * operatore di dereferenziamento.     * @return il dato "puntato" dall'iteratore.     */     reference operator*() const {         return *ptr;     }        /**     * operatore freccia.      *      * @return il puntatore al dato "puntato" dall'iteratore.     */     pointer operator->() const {         return ptr;     }            /**     * operatore di confronto (uguaglianza).      *      * @param other iteratore da confrontare     * @return true se due iteratori "puntano" allo stesso dato     */     bool operator==(const iterator &other) const {         return ptr == other.ptr;     }        /**     * operatore di confronto (disuguaglianza).      *      * @param other iteratore da confrontare     * @return true se due iteratori "puntano" dati diversi     */     bool operator!=(const iterator &other) const {         return !(*this == other);     }        /**     * operatore di confronto (uguaglianza).      *      * @param other const_iteratore da confrontare     * @return true se due iteratori "puntano" allo stesso dato     */     bool operator==(const const_iterator &other) const {         return ptr == other.ptr; // vedi const_iterator     }        /**     * operatore di confronto (uguaglianza).      *      * @param other const_iteratore da confrontare     * @return true se due iteratori "puntano" dati diversi     */     bool operator!=(const const_iterator &other) const {         return !(*this == other);     }        /**     * operatore di "elemento successivo". versione pre-incremento.      *      * @return l'iteratore che "punta" all'elemento successivo     */     iterator &operator++() {         ++ptr;         return *this;     }        /**     * operatore di "elemento successivo". versione post-incremento.      *      * @return l'iteratore che "punta" all'elemento successivo     */     iterator operator++(int) {         iterator tmp(ptr);         ++ptr;         return tmp;     } private:     value_type *ptr;        /**     * costruttore di inizializzazione.     * @param p puntatore ai dati     */     explicit iterator(value_type *p) : ptr(p){} }; // iterator     /**     * inizio const iterator     */ class const_iterator {     friend class iterator;       friend class sparsematrix;  public:            typedef std::forward_iterator_tag iterator_category;        typedef t value_type;        typedef ptrdiff_t difference_type;        typedef const t* pointer;        typedef const t& reference;      const_iterator() : ptr(0) {}      const_iterator(const const_iterator &other) : ptr(other.ptr) {}      const_iterator& operator=(const const_iterator &other) {         ptr = other.ptr;         return *this;     }     ~const_iterator() {}        /**     * costruttore di conversione.     * @param other iterator da convertire     */     const_iterator(const iterator &other) : ptr(other.ptr) {}        /**     * operatore di dereferenziamento.     * @return il dato "puntato" dall'iteratore.     */     reference operator*() const {         return *ptr;     }        /**     * operatore freccia.     * @return il puntatore al dato "puntato" dall'iteratore.     */     pointer operator->() const {         return ptr;     }        /**     * operatore di confronto (uguaglianza).     * @param other const_iteratore da confrontare     * @return true se due iteratori "puntano" allo stesso dato     */     bool operator==(const const_iterator &other) const {         return ptr == other.ptr;     }        /**     * operatore di confronto (disuguaglianza).     * @param other const_iteratore da confrontare     * @return true se due iteratori "puntano" dati diversi     */     bool operator!=(const const_iterator &other) const {         return !(*this == other);     }        /**     * operatore di "elemento successivo". versione pre-incremento.     * @return l'iteratore che "punta" all'elemento successivo     */     const_iterator &operator++() {         ++ptr;         return *this;     }            /**     * operatore di "elemento successivo". versione post-incremento.     * @return l'iteratore che "punta" all'elemento successivo     */     const_iterator operator++(int) {         const_iterator tmp(ptr);         ++ptr;         return tmp;     } private:     const t *ptr;        /**     * costruttore di inizializzazione.     * @param p puntatore ai dati     */     explicit const_iterator(const t *p) : ptr(p)  {} }; // const_iterator  iterator begin() {     return iterator(val); }  iterator end() {     return iterator(val + size); }  const_iterator begin() const {     return const_iterator(val); }  const_iterator end() const {     return const_iterator(val + size); } 

how must return struct? sorry horrible english!

you can return structs functions explicitly in c(++). can't follow example (sorry!), define return type struct. example:

typedef struct  {         int a, b, c; } my_struct_type;  my_struct_type returnstruct(my_struct_type example) {    my_struct_type returnvalues;    returnvalues.a=example.a*2;    returnvalues.b=example.b*2;    returnvalues.c=example.c*2;     return(returnvalues); }      int main() { struct my_struct_type example;  example.a=5; example.b=10; example.c=15;   example=returnstruct(example); //a=10; b=20; c=30 } 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -