[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;
}