程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 深入設模式之:迭代器模式之編寫一個兼容STL 算法的Iterator

深入設模式之:迭代器模式之編寫一個兼容STL 算法的Iterator

編輯:C++入門知識

[cpp]
#include <vector>  
#include <algorithm>  
 
class Seat 

public: 
Seat(int no):no_(no) {} 
int no_; 
}; 
 
 
class Table 

static const std::size_t BEGIN_SEAT = 1; 
static const std::size_t INVALID_SEAT = 0; 
static const std::size_t END_SEAT = 9; 
public: 
Table() 

seats_.push_back(new Seat(1) ); 
seats_.push_back(new Seat(2) ); 
seats_.push_back(new Seat(3) ); 
seats_.push_back(new Seat(4) ); 
seats_.push_back(new Seat(5) ); 
seats_.push_back(new Seat(6) ); 
seats_.push_back(new Seat(7) ); 
seats_.push_back(new Seat(8) ); 
seats_.push_back(new Seat(9) ); 

 
 
std::vector<Seat *> seats_; 
 
 
Seat * get( const int seat ) 

return seats_.at(seat - 1); 

 
 
public: 
class Iterator  

friend class CycleIterator; 
public: 
typedef size_t size_type; 
typedef ptrdiff_t difference_type; 
 
 
typedef std::forward_iterator_tag iterator_category; 
typedef Seat *        value_type; 
typedef Seat **           pointer; 
typedef Seat *&         reference; 
 
 
Iterator(Table * table, const int position):table_(table), position_(position) {} 
 
 
Iterator(const Iterator & it):table_(it.table_), position_(it.position_) {} 
 
 
Iterator operator ++() 

for (++position_; position_ <= Table::END_SEAT;++position_ ) 

//if (table_->get(position_)->visible_ == true)  

return Iterator(table_, position_); 


 
 
position_ = Table::INVALID_SEAT; 
 
 
return Iterator(table_, Table::INVALID_SEAT); 

 
 
Seat * operator ->() 

return table_->get(position_); 

 
 
Seat * operator *() 

return table_->get(position_); 

 
 
bool operator == (const Iterator & it)const 

return position_ == it.position_; 

 
 
bool operator != (const Iterator & it)const 

return position_ != it.position_; 

 
 
private: 
Table * table_; 
 
 
int position_; 
}; 
 
 
public: 
Iterator begin() 

return Iterator(this, BEGIN_SEAT); 

 
 
Iterator end() 

return Iterator(this, INVALID_SEAT); 

}; 
 
 
bool FindNullSeat(Seat *  seat) 

return seat->no_ == 3; 

 
 
int main(int , char **) 

Table table; 
Table::Iterator seat = std::find_if(table.begin(), table.end(), FindNullSeat); 
 
 
return 0; 

#include <vector>
#include <algorithm>

class Seat
{
public:
Seat(int no):no_(no) {}
int no_;
};


class Table
{
static const std::size_t BEGIN_SEAT = 1;
static const std::size_t INVALID_SEAT = 0;
static const std::size_t END_SEAT = 9;
public:
Table()
{
seats_.push_back(new Seat(1) );
seats_.push_back(new Seat(2) );
seats_.push_back(new Seat(3) );
seats_.push_back(new Seat(4) );
seats_.push_back(new Seat(5) );
seats_.push_back(new Seat(6) );
seats_.push_back(new Seat(7) );
seats_.push_back(new Seat(8) );
seats_.push_back(new Seat(9) );
}


std::vector<Seat *> seats_;


Seat * get( const int seat )
{
return seats_.at(seat - 1);
}


public:
class Iterator
{
friend class CycleIterator;
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;


typedef std::forward_iterator_tag iterator_category;
typedef Seat *        value_type;
typedef Seat **           pointer;
typedef Seat *&         reference;


Iterator(Table * table, const int position):table_(table), position_(position) {}


Iterator(const Iterator & it):table_(it.table_), position_(it.position_) {}


Iterator operator ++()
{
for (++position_; position_ <= Table::END_SEAT;++position_ )
{
//if (table_->get(position_)->visible_ == true)
{
return Iterator(table_, position_);
}
}


position_ = Table::INVALID_SEAT;


return Iterator(table_, Table::INVALID_SEAT);
}


Seat * operator ->()
{
return table_->get(position_);
}


Seat * operator *()
{
return table_->get(position_);
}


bool operator == (const Iterator & it)const
{
return position_ == it.position_;
}


bool operator != (const Iterator & it)const
{
return position_ != it.position_;
}


private:
Table * table_;


int position_;
};


public:
Iterator begin()
{
return Iterator(this, BEGIN_SEAT);
}


Iterator end()
{
return Iterator(this, INVALID_SEAT);
}
};


bool FindNullSeat(Seat *  seat)
{
return seat->no_ == 3;
}


int main(int , char **)
{
Table table;
Table::Iterator seat = std::find_if(table.begin(), table.end(), FindNullSeat);


return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved