簡體   English   中英

堆棧上的前向迭代器

[英]Forward Iterator on a Stack

我必須在基於數組的堆棧上實現一個前向迭代器。 我不能使用 std::vectors 或任何東西,我只需要它。 當我開始使用前向迭代器,特別是運算符時,我對這個程序的開發就停止了。

我有一個方法,它采用一個通用序列,從中,給定一個偏移量,創建一個堆棧:

template <typename IterT>       
  stack(IterT begin, IterT end) : _stack(0), _size(0), _capacity(0) {
    try {       
        for(; begin!=end; ++begin) {
            push(static_cast<T>(*begin));       
        }
    }
    catch(...) {
        clear();   //my method to destroy the stack 
        throw;
    }
}

在我的主要工作中,我執行以下操作:

int a[5] = {1, 2, 3, 4, 5};
stack<int> sint(a, a+5);
cout << sint << endl;

但是當代碼運行時,堆棧被創建但不打印。 有人可以幫助我嗎? 並且還給我其他幫助(關於代碼縮進、改進等...)謝謝,我會轉發迭代器代碼。

 class const_iterator {
    const T* data;
    unsigned int index;
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() : data(0){}

    const_iterator(const T* arr) : data(arr) {}

    const_iterator(const const_iterator &other) 
        : data(other.data){ }

    const_iterator& operator=(const const_iterator &other) {
        data = other.data;
        return *this;
    }

    ~const_iterator() {
        data = 0;
    }

    reference operator*() const {
        return *data;
    }

    pointer operator->() const {
        return &(data);
    }

    const_iterator operator++(int) {
        const_iterator tmp(*this);
        ++*this;
        return tmp;
    }

    const_iterator& operator++() {
        ++data;
        return *this;
    }

    bool operator==(const const_iterator &other) const {
        return data[index] == other.data[index];
    }

    bool operator!=(const const_iterator &other) const {
        return data[index] != other.data[index] ;
    }


private:

    friend class stack; 

    const_iterator(unsigned int ind) :
        index(ind){}

}; // class const_iterator

const_iterator begin() const {
    cout << "begin" << _stack[_size-1] << endl;
    return const_iterator(_stack[_size-1]);
}

const_iterator end() const {
    cout << "end" << _stack[0] << endl;
    return const_iterator(_stack[0]);
}

最后但並非最不重要的是,我重新定義了 << 運算符以適合迭代器:

template <typename T>
std::ostream &operator<<(std::ostream &os, const stack<T> &st) {
typename stack<T>::const_iterator i, ie;
for(i = st.begin(), ie = st.end(); i!=ie; ++i){
    os << *i << std::endl;
}
return os;
}

堆棧的代碼如下(為了便於閱讀,我省略了一些內容)。

stack()
    : _capacity(0), _size(0), _stack(0){}
void push (const T &value){
    if (_size == _capacity){    //raddoppio la dimensione 
        if(_capacity == 0)
            ++_capacity;
        _capacity *= 2;
        T* tmp = new T[_capacity];
        copy_n(_stack, _size, tmp);
        swap(_stack, tmp);
        delete[] tmp;
    }
    _stack[_size] = value;
    ++_size;
}

void pop(){
    T _tmp;
    if(!is_empty()){
        _tmp = _stack[_size-1];
        --_size;
    }
} 

如果你想創建一個看起來像指針的迭代器,你不需要index ,因為data扮演了它的角色。 比較運算符應該比較data ,而不是值:

bool operator==(const const_iterator &other) const {
    return data == other.data;
}

如果要創建反向迭代器,則稍微復雜一些。 首先, operator++應該減少data 其次,解引用運算符不應該返回*data ,而是返回*(data - 1) 第三, databegin()迭代應指向stack[size] ,並且dataend()迭代應指向stack[0] 在任何情況下都不需要析構函數。

我遵循了之前的建議,這是編輯后的結果,但我仍然無法弄清楚如何正確使用私有部分中的構造函數

class const_iterator {
    const T *data;
public:
    /* ITERATOR TRAITS HERE */

    const_iterator() : data(0){}

    const_iterator(const T* arr) : data(arr) {}

    const_iterator(const const_iterator &other) 
        : data(other.data){ }

    const_iterator& operator=(const const_iterator &other) {
        data = other.data;
        return *this;
    }

    ~const_iterator() {
        data = 0;
    }

    reference operator*() const {
        return *data;
    }

    pointer operator->() const {
        return &(data);
    }

    const_iterator operator++(int) {
        const_iterator tmp(*this);
        ++*this;
        return tmp;
    }

    const_iterator& operator++() {
        ++data;
        return *this;
    }

    bool operator==(const const_iterator &other) const {
        return data == other.data;
    }

    bool operator!=(const const_iterator &other) const {
        return data != other.data;
    }

private:
    friend class stack; 

    const_iterator(const T *d) {
        data = d;
    }

}; // classe const_iterator

const_iterator begin() const {
    return const_iterator(_stack[_size-1]);
}

const_iterator end() const {
    return const_iterator(_stack[0]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM