1D 배열 (템플릿으로 표시)로 표시되는 모눈 인 컨테이너를 디자인하고 있습니다. 여기에 코드 추출물을 게시 할 예정입니다. 실제로 더 많은 코드가 있습니다. 로봇 애플리케이션에서 회전하는 점유 격자로 사용되며 각 셀은 세계의 작은 영역을 나타냅니다. 내가 그 그리드 자주 할사용자 지정 컨테이너에 대한 사용자 지정 반복자
한 작업이 모든 셀을 통과하며 자신의 세계를 검색하는 것입니다 좌표 :
for(unsigned r=0; r<mygrid.rows_; ++r) {
for(unsigned c=0; c<mygrid.cols_; ++c) {
cell = mygrid.getRC(r,c);
mygrid.rcToXY(r,c,&x,&y);
}
}
나는 상점보다 반복자이 모든 싶은 : 셀을, 그 rc 좌표와 그 xy 좌표.
for(Grid<CellType>::const_iterator it=mygrid.begin(); it!=mygrid.end(); ++it) {
cell = *it;
printf("%d %d %f %f\n", it.r(), it.c(), it.x(), it.y());
}
온라인으로 많은 답변과 튜토리얼을 작성한 후 작동하는 다음 구현을 생각해 냈습니다. 그러나 그것은 나에게 조금 서투른 것처럼 보이고, 학문을 위해서 나는 그것을 더 좋아 보이게하고 싶습니다. 또한 STL 호환성도 좋습니다.
template <class G, typename C>
class base_iterator
{
private:
G* grid_;
C* cell_;
unsigned r_, c_; // local
double x_, y_;
// this should be private with access for friends (Grid) only
// but I can't make it work
public:
base_iterator(G* grid, unsigned r, unsigned c) : grid_(grid), r_(r), c_(c)
{
cell_ = (r<grid->rows_ && c<grid->cols_) ? &grid_->getRC(r,c) : 0;
grid_->rcToXY(r,c,&x_,&y_);
}
public:
base_iterator() : grid_(0) { }
// used to cast an iterator to a const_iterator
template <class G2, typename C2>
base_iterator(const base_iterator<G2,C2>& other)
{
grid_ = other.grid();
cell_ = & other.cell();
r_ = other.r();
c_ = other.c();
x_ = other.x();
y_ = other.y();
}
// this should be private with access for friends only
G* grid() const { return grid_; }
C& cell() { return *cell_; }
const C& cell() const { return *cell_; }
unsigned r() const { return r_; }
unsigned c() const { return c_; }
double x() const { return x_; }
double y() const { return y_; }
C* operator->() { return cell_; }
const C* operator->() const { return cell_; }
C& operator*() { return *cell_; }
const C& operator*() const { return *cell_; }
//prefix
base_iterator & operator++()
{
// my iteration logic here which needs access to grid
// in order to find the number of rows, etc.
return *this;
}
//postfix
base_iterator operator++(int)
{
base_iterator it(*this); // make a copy for result
++(*this); // Now use the prefix version to do the work
return it; // return the copy (the old) value.
}
template <class G2, typename C2>
bool operator==(const base_iterator<G2,C2> & other) const
{
return cell_ == &other.cell();
}
template <class G2, typename C2>
bool operator!=(const base_iterator<G2,C2>& other) const
{ return cell_!=other.cell(); }
};
그리고 내 그리드 클래스 :
typedef base_iterator<Grid<T>,T> iterator;
typedef base_iterator<Grid<T> const, T const> const_iterator;
iterator begin() { return iterator(this,0,0); }
iterator end() { return iterator(this,rows_,cols_); }
const_iterator begin() const { return const_iterator(this,0,0); }
const_iterator end() const { return const_iterator(this,rows_,cols_); }
다시 말하지만,이 작품,하지만 난 그게 조금 서투른 느낌 (반복자 코드의 주석을 참조), 내가하고 싶습니다 내가 그것을 향상시킬 수있는 방법을 안다. 부스트 반복기 외관이나 어댑터를 사용하는 것에 대한 수많은 게시물을 보았지만이를 필자의 경우에 적용하는 방법을 알 수 없었다.