我将装有自定集装箱的 st集装箱延伸,以便更灵活地控制部件的运行。
class MyContainer;
template <typename T> class myiterator :public iterator<bidirectional_iterator_tag, T>
{
friend class MyContainer;
private:
T *pointer;
myiterator(T *pt):pointer(pt) {}
public:
T& operator*() {return (*pointer);}
const myiterator<T>& operator++()
{
pointer->current_iterator++;
return *this;
}
bool isEnd(void) const
{
return pointer->current_iterator == pointer->data.end();
}
};
class MyContainer
{
friend class myiterator<MyContainer>;
public:
typedef myiterator<MyContainer> iterator;
typedef myiterator<MyContainer const> const_iterator;
private:
map<int, int> data;
map<int, int>::const_iterator current_iterator;
public:
MyContainer() {current_iterator = data.begin(); }
void addDataPair(int key, int value) {data[key] = value;}
int first() const {return (*current_iterator).first;}
int second() const {return (*current_iterator).second;}
iterator begin()
{
current_iterator = data.begin();
return iterator(this);
}
const_iterator begin() const
{
return const_iterator(this);
}
};
如果我使用激光器作为跟踪器,则该代码行,。
MyContainer h;
h.addDataPair(1, 1);
h.addDataPair(2, 2);
h.addDataPair(3, 3);
for (MyContainer::iterator it=h.begin(); !it.isEnd(); ++it)
{
cout << (*it).first() << " " << (*it).second() << endl;
}
但是,如果我改变召集人,它就赢得了汇编。 我读过一些条款,其中提到,为了确定固定的炉子,我们只是要取代从X到X星等值,这是我在守则中所做的。 但是,我很快发现,在我的案件中,它可能赢得一些工作,因为在我的案件中,一个炉子的回击是集装箱本身。 我当时不知道如何使召集人的工作不重复编码。
In addition, my iterator is derived from std::iterator but I found that I cannot override the constructor for my iterator. Is that any way that I can pass more than one parameters into my iterator besides the T *pt ? Thanks.