有关自定义迭代器的问题

sunyuqian 2012-12-02 07:40:49
大家好,这个程序有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;
}
...全文
145 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunyuqian 2012-12-03
  • 打赏
  • 举报
回复
谢谢了谢谢了
_程序员 2012-12-03
  • 打赏
  • 举报
回复
想终止? 可以设置数组(类似)最后一个元素之后的指针为一个标识,遍历到那里就终止。 62708807 职业C/C++交流群 以前很火的,后来被群主清空了 现在找人
sunyuqian 2012-12-03
  • 打赏
  • 举报
回复
pos == ln->end() ln->end()返回的应该是字符啊
sunyuqian 2012-12-02
  • 打赏
  • 举报
回复
print没有错吧
Text_iterator& Text_iterator::operator++()
{
//为什么不可以pos.end()

//我写
//Line l
//l.end()就可以
	if(*pos =='\n')
	{
		++ln;
		pos = ln->begin();
	}
	else
		++pos;
	return *this;
}
漫步者、 2012-12-02
  • 打赏
  • 举报
回复
引用 5 楼 ganpengjin1 的回复:
C/C++ code??12Text_iterator p=d.begin(); p!=d.end(); ++p) cout<<*p;} 你的Document 和 Text_iterator 是包含的关系,不能这样写,你再想想!!
说错了,是组合的关系!!
漫步者、 2012-12-02
  • 打赏
  • 举报
回复
Text_iterator p=d.begin(); p!=d.end(); ++p) cout<<*p;
} 你的Document 和 Text_iterator 是包含的关系,不能这样写,你再想想!!
漫步者、 2012-12-02
  • 打赏
  • 举报
回复
引用 3 楼 sunyuqian 的回复:
is.good() 不还是得按ctrl+z结束吗 print(d);还是无法执行
可以结束输入,你的print有问题
sunyuqian 2012-12-02
  • 打赏
  • 举报
回复
is.good() 不还是得按ctrl+z结束吗 print(d);还是无法执行
prajna 2012-12-02
  • 打赏
  • 举报
回复
    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() //可用這個判斷終止
sunyuqian 2012-12-02
  • 打赏
  • 举报
回复
#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);
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧