读书人

有关自定义迭代器的有关问题

发布时间: 2012-12-14 10:33:07 作者: rapoo

有关自定义迭代器的问题
大家好,这个程序有2个问题,请高手解答

#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;
}
[最优解释]
list<Line>::iterator?ln;
????Line::iterator?pos;

//if(pos == ln->end())没有执行,如果换成

ln->end() --- Line
pos --- char

char ch;
while(is.get(ch))
{
d.line.back().push_back(ch);
if(ch == '\n')
d.line.push_back(Line());
}
//如何中止它,必须设定特殊字符吗

is.good() //可用判止

[其他解释]

Text_iterator p=d.begin(); p!=d.end(); ++p) cout<<*p;
} 你的Document 和 Text_iterator 是包含的关系,不能这样写,你再想想!!

[其他解释]
想终止?
可以设置数组(类似)最后一个元素之后的指针为一个标识,遍历到那里就终止。
62708807
职业C/C++交流群
以前很火的,后来被群主清空了
现在找人
[其他解释]
#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);
}


[其他解释]
is.good() 不还是得按ctrl+z结束吗

print(d);还是无法执行
[其他解释]
引用:
is.good() 不还是得按ctrl+z结束吗

print(d);还是无法执行

可以结束输入,你的print有问题
[其他解释]
引用:
C/C++ code??12Text_iterator p=d.begin(); p!=d.end(); ++p) cout<<*p;} 你的Document 和 Text_iterator 是包含的关系,不能这样写,你再想想!!
说错了,是组合的关系!!
[其他解释]
print没有错吧

Text_iterator& Text_iterator::operator++()
{
//为什么不可以pos.end()

//我写
//Line l
//l.end()就可以
if(*pos =='\n')
{
++ln;
pos = ln->begin();
}
else
++pos;
return *this;
}

[其他解释]
pos == ln->end()

ln->end()返回的应该是字符啊
[其他解释]
谢谢了谢谢了

读书人网 >C++

热点推荐