我想实现一个迭代器,迭代反单调区间,这样的区间包含反单调函数。因此迭代器必须遍历间隔并返回反单调函数。必须从顶部或底部元素动态计算函数。
class AMFInterval {
public:
class AMFIterator : public iterator<forward_iterator_tag, AMFunction> {
friend class AMFunction;
public:
AMFunction* amf;
AMFIterator(AMFInterval interval) {amf = &interval.from;};
const reference operator*() {return *amf;}
};
private:
AMFunction from;
AMFunction till;
typedef AMFIterator iterator;
public:
AMFInterval(AMFunction bottom, AMFunction top);
virtual ~AMFInterval();
//iterator
iterator begin() {return iterator(*this);};
iterator end() {return iterator(*this);};
// query
string toString();
// class
static AMFInterval fullspace(int n);
};
我还添加了解析器的代码:
AMFunction Parser::parse_amf(string a) {
int size = a.size();
AMFunction amf;
bool sbs = false;
SmallBasicSet s;
for (int i = 0 ; i < size ; i++) {
if (sbs && a[i] != ']' && a[i] != ',' && a[i] != ' ') {
s.quickadd(a[i] - '0');
} else if ( a[i] == '[' ) {
sbs = true;
s = SmallBasicSet();
} else if ( a[i] == ']' ) {
sbs = false;
amf.addSet(s);
}
}
return amf;
}
如果我们测试迭代器,我们得到“ EXC_BAD_ACCESS(code = 1,address = 0x0)”,它不能返回sting b,但如果我调试,正确的值是在指针,这是测试:
void test_iterator() {
Parser p;
AMFInterval amf(p.parse_amf("{[1]}"), p.parse_amf("{}"));
AMFInterval::AMFIterator freddy = amf.begin();
//cout << p.parse_amf("{[1,2,3]}").toString() << endl;
AMFunction &b = *(freddy.amf);
cout << b.toString();
//cout << (*amf.end()).toString() << endl;
}