《Essential C++》系列笔记之第四章(基于对象的编程风格)之第六节(打造一个Iterator Class)

《Essential C++》系列笔记之第四章(基于对象的编程风格)之第六节(打造一个Iterator Class)_第1张图片
代码实践

  • Triangular.h

    #pragma once
    #include 
    #include 
    #include "Triangular_iterator.h"
    using namespace std;
    
    class Triangular_iterator;
    class Triangular
    {
    	friend class Triangular_iterator; //把Triangular_iterator声明为友元类,记得包含类的头文件
    public:
    	typedef Triangular_iterator iteraotr;
    
    	/*Triangular_iterator begin() const
    	{
    		return Triangular_iterator(_beg_pos);
    	}*/
    	//Triangular_iterator a;
    
    	Triangular(const Triangular&);
    	Triangular(int, int);
    
    	int lenth() const { return _length; }
    	int beg_pos() const { return _beg_pos; }
    	int elem(int) const;
    
    	void next_reset() const{ _next = _beg_pos - 1; }
    	bool next(int& val) const;
    
    	int sum(const Triangular&);
    
    private:
    	string _name;
    	int _length; //元素个数
    	int _beg_pos; //起始位置
    	mutable int _next;   //下一个迭代目标
    	static vector<int> _elems;
    };
    
    
  • Triangular.cpp

    #include "Triangular.h"
    
    vector<int> Triangular::_elems; //static data member只有唯一实体,必须在cpp中提供清楚的定义
    
    Triangular::Triangular(const Triangular& rhs)
    	:_length(rhs._length), _beg_pos(rhs._beg_pos), _next(rhs._next)
    {}
    
    Triangular::Triangular(int len, int bp)
    	: _name("Triangular")
    {
    	this->_length = (len > 0 ? len : 1);
    	this->_beg_pos = (bp > 0 ? len : 1);
    	this->_next = this->_beg_pos - 1;
    }
    
    int Triangular::elem(int pos) const
    {
    	return _elems[pos - 1];
    }
    
    bool Triangular::next(int& val) const
    {
    	if ( _next < _beg_pos + _length - 1 )
    	{
    		val = _elems[_next++];
    		return true;
    	}
    	return false;
    }
    
    int Triangular::sum(const Triangular& trian)
    {
    	return 0; //只是测试
    }
    
  • Triangular_iterator.h

    #pragma once
    #include "Triangular.h"
    class Triangular_iterator
    {
    	friend class Triangular;
    public:
    	Triangular_iterator(int index) : _index(index - 1) {}
    	bool operator==(const Triangular_iterator&)const;
    	bool operator!=(const Triangular_iterator&)const;
    	int operator*()const;
    	Triangular_iterator& operator++();
    	Triangular_iterator& operator++(int);
    private:
    	void check_integrity() const;
    	int _index;
    };
    
    
  • Triangular_iterator.cpp

    #include "Triangular_iterator.h"
    
    //vector Triangular::_elems; //static data member只有唯一实体,必须在cpp中提供清楚的定义
    bool Triangular_iterator::operator==(const Triangular_iterator&rhs) const
    {
    	return _index == rhs._index;
    }
    
    bool Triangular_iterator::operator!=(const Triangular_iterator&rhs) const
    {
    	return  !(*this == rhs); 
    }
    
    int Triangular_iterator::operator*() const
    {
    	//check_integrity();
    	return Triangular::_elems[_index];
    }
    
    

今天是20200324 我一定会回来的!

你可能感兴趣的:(《Essential,C++》系列笔记)