指针和迭代器 C++

pointer & iterator c++

本文关键字:C++ 迭代器 指针      更新时间:2023-10-16

我必须做一个项目。我有一个稀疏矩阵,有3个数组:

  1. val
  2. 行位置的pr
  3. 立柱位置的pc

现在我遇到了迭代器的问题。我必须返回一个指向包含3个内容的结构的指针:

  1. 价值
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 i 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 i due iteratori "puntano" a dati diversi
    */
    bool operator!=(const iterator &other) const {
        return !(*this == other);
    }
       /**
    * Operatore di confronto (uguaglianza). 
    * 
    * @param other const_iteratore da confrontare
    * @return true se i 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 i due iteratori "puntano" a 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 i 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 i due iteratori "puntano" a 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);
}

返回一个结构必须怎么做?对不起我糟糕的英语!

您可以在C(++)中显式地从函数返回structs。我不能简单地按照你的例子(对不起!),但你只需要将它们的返回类型定义为你的结构。例如:

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
}