64,654
社区成员
发帖
与我相关
我的任务
分享
Text_iterator& Text_iterator::operator++()
{
//为什么不可以pos.end()
//我写
//Line l
//l.end()就可以
if(*pos =='\n')
{
++ln;
pos = ln->begin();
}
else
++pos;
return *this;
}
Text_iterator p=d.begin(); p!=d.end(); ++p) cout<<*p;
} 你的Document 和 Text_iterator 是包含的关系,不能这样写,你再想想!!
#include <vector>
#include <list>
#include <istream>
using namespace std;
typedef vector<char> Line;
class Text_iterator
{
list<Line>::iterator ln;
Line::iterator pos;
public:
Text_iterator(list<Line>::iterator ii,Line::iterator pp):ln(ii),pos(pp){}
char& operator*(){return *pos;}
Text_iterator& operator++();
bool operator==(const Text_iterator& other)const {return ln == other.ln && pos == other.pos;}
bool operator!=(const Text_iterator& other)const {return !(*this == other);}
};
Text_iterator& Text_iterator::operator++()
{
//if(pos == ln->end())没有执行,如果换成
//if(*pos == '\n')就可以
if(pos == ln->end())
{
++ln;
pos = ln->begin();
}
else
++pos;
return *this;
}
struct Document
{
list<Line> line;
Document(){line.push_back(Line());}
Text_iterator begin()
{
return Text_iterator(line.begin(),line.begin()->begin());
}
Text_iterator end()
{
list<Line>::iterator last = line.end();
--last;
return Text_iterator(last,last->end());
}
};
istream& operator>>(istream& is,Document& d)
{
char ch;
while(is.get(ch))
{
d.line.back().push_back(ch);
if(ch == '\n')
d.line.push_back(Line());
}
//如何中止它,必须设定特殊字符吗
return is;
}
void print(Document& d)
{
for(Text_iterator p=d.begin(); p!=d.end(); ++p) cout<<*p;
}
int main()
{
Document d;
cin>>d;
print(d);
}