我希望了解以下迭代器类的示例。做什么input_iterator_tag
?mit.p
用作构造函数中使用的值,但不是指针mit.p
?是什么*this
?如果构造函数只允许整数和指针/值引用,是否myiterator beginning(numbers)
允许?
// iterator example
#include <iostream>
#include <iterator>
using namespace std;
class myiterator : public iterator<input_iterator_tag, int> // what is input_iterator_tag?
{
int* p;
public:
myiterator(int* x) :p(x) {}
myiterator(const myiterator& mit) : p(mit.p) {} // why isn't this p(*mit.p)?
myiterator& operator++() {++p;return *this;} // what is *this?
myiterator operator++(int) {myiterator tmp(*this); operator++(); return tmp;}
bool operator==(const myiterator& rhs) {return p==rhs.p;}
bool operator!=(const myiterator& rhs) {return p!=rhs.p;}
int& operator*() {return *p;}
};
int main () {
int numbers[]={10,20,30,40,50};
myiterator beginning(numbers); // however, constructor is not overloaded with array
myiterator end(numbers+5);
for (myiterator it=beginning; it!=end; it++)
cout << *it << " ";
cout << endl;
return 0;
}