小编HBY*_*4PI的帖子

像写迭代器一样编写STL

我试图学习像迭代器一样编写stl,因为我写了一个简单的循环数组并在其中添加了一个迭代器.请查看代码底部以查看问题.

template<typename T, int N>
class RingQueue{
    T * _marray;
    int _mbegin;
    int _msize;
public:
    RingQueue(){
        _marray = new T[N];
        _mbegin = 0;
        _msize= 0;
    }   
    void push_back(const T& val){
        if(_msize!=N){
            _marray[(_mbegin+_msize)%N] = val;
            _msize++;
        }
        else
            throw "Queue Full";
    }   
    T pop_front(){
        if(_msize!=0){
            T&val = _marray[_mbegin];
            _mbegin = (_mbegin+1)%N;
            _msize--;
            return val;
        }
        else
            throw "Queue Empty";
    }

    class iterator{
        RingQueue<T,N>* _container;
        int _idx;
        public:
        iterator(RingQueue<T,N>* container,int idx):_container(container){
            _idx = idx;
        }

        bool operator==(iterator &rhs){
            return (this->_container==rhs._container && this->_idx …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl operators data-structures

4
推荐指数
1
解决办法
437
查看次数

标签 统计

c++ ×1

data-structures ×1

iterator ×1

operators ×1

stl ×1