【发布时间】:2020-05-11 07:03:42
【问题描述】:
在下面的代码中,输出是“wrhrwwr”,我试图理解迭代器在做什么,以及“++”是如何被重载的。似乎它以某种方式跳过了“e”。但是,代码对我来说非常不清楚,也许我可以得到帮助。 谢谢
#include <iostream>
using namespace std;
class datas {
private:
const char *string,marker;
const int len;
public:
class hophop {
private:
const char *pos, next;
public:
char operator *() { return *pos; }
hophop operator ++() {++pos;while (*(pos+1)!=next) ++pos; return *this; }
bool operator !=(hophop &o) { return pos < o.pos; }
hophop(const char *p, char m) : pos(p),next(m) {}
};
typedef hophop iterator;
iterator begin() { return hophop (string, marker); }
itrator end () { return hophop(string +len ,marker); }
datas(const char *d,int l, char m) : string (d), len(l),marker(m){}
};
int main( void ) {
datas chars ("we are where we were", 20, 'e');
for (char c: chars)
cout << c;
return 0;
}
【问题讨论】:
标签: c++ pointers iterator operator-overloading