自定义迭代器而不是解释问题
Custom iterator not dereferencing issue
这是将迭代器实现至 std::list<std::vector<char>>
的首次尝试:
Document.h
#ifndef Document_h
#define Document_h
//-------------------------------------------------------------------------
typedef std::vector<char> Line; // line of text
//-------------------------------------------------------------------------
class Text_iterator
{
public:
Text_iterator(std::list<Line>::iterator l, Line::iterator p)// constructor
: ln(l), pos(p) { }
Text_iterator(const Text_iterator& src) // copy constructor
: ln(src.ln), pos(src.pos) { }
Text_iterator& operator= (const Text_iterator& src) // copy assignment
{
Text_iterator temp(src);
this->swap(temp);
return *this;
}
char& operator*() { return *pos; } // dereferencing
Text_iterator& operator++ () // incrementation
{
++pos;
if (pos == ln->end())
{
++ln;
pos = ln->begin();
}
return *this;
}
bool operator== (const Text_iterator& other) const // comparison
{
return ln == other.ln && pos == other.pos;
}
bool operator != (const Text_iterator& other) const // comparison
{
return !(*this == other);
}
void swap(Text_iterator& src) // helper: swap
{
std::swap(src.get_line(), ln);
std::swap(src.get_column(), pos);
}
std::list<Line>::iterator get_line() { return ln; } // accessors
Line::iterator get_column() { return pos; }
private:
std::list<Line>::iterator ln; // data members
Line::iterator pos;
};
//-------------------------------------------------------------------------
void swap (Text_iterator& lhs, Text_iterator& rhs) // object swap
{
lhs.swap(rhs);
}
//-------------------------------------------------------------------------
class Document
{
public:
typedef Text_iterator iterator;
public:
Document() // constructor
{
Line l(10, 'a');
text.push_back(l);
}
iterator begin() // iterator to first element
{
return iterator(text.begin(), (*text.begin()).begin());
}
iterator end() // iterator to last element
{
return iterator(text.end(), (*text.end()).end());
}
void print()
{
for (Document::iterator p = begin(); p != end(); ++p)
{
std::cout << *p;
getchar();
}
}
std::list<Line> text; // data member
};
#endif
main.cpp
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <algorithm>
#include "Document.h"
int main()
{
Document text;
text.print();
}
预期输出:
aaaaaaaaaaa
而不是上述预期输出,我得到了:
调试断言失败
表达式:列表迭代器不可用。
为什么我得到这种行为以及如何纠正?
注意:经过简短的研究,我发现这种行为的最常见原因是尝试指定end()
迭代器,但是我在代码中找不到这种表达。
您在 Document::end()
中删除了结束迭代器*text.end()
。最简单的修复方法是使用list::back()
(和Document::begin()
中的list::front()
)。
解决此问题后,您会发现Text_iterator::operator++
还将放置结束迭代器,因为您不会在适当的端情况下检查ln
。@Jonathan Potter的评论是对的,您需要将text.end()
传递给两个Text_iterator
S
更改:
class Text_iterator
{
// Declarations elided
private:
std::list<Line>::iterator ln;
std::list<Line>::iterator ln_end;
Line::iterator pos;
}
Text_iterator::Text_iterator(std::list<Line>::iterator l, std::list<Line>::iterator l_end, Line::iterator p)
: ln(l), ln_end(l_end), pos(p) { }
Text_iterator::Text_iterator(const Text_iterator& src)
: ln(src.ln), ln_end(src.ln_end), pos(src.pos) { }
Text_iterator& Text_iterator::operator++ ()
{
++pos;
if (pos == ln->end())
{
++ln;
if(ln != ln_end)
{
pos = ln->begin();
}
}
return *this;
}
void Text_iterator::swap(Text_iterator& src)
{
std::swap(src.ln, ln);
std::swap(src.ln_end, ln_end);
std::swap(src.pos, pos);
}
Document::iterator Document::begin()
{
return iterator(text.begin(), text.end(), text.front().begin());
}
Document::iterator Document::end()
{
return iterator(text.end(), text.end(), text.back().end());
}
当最终增量发生时,POS将指向最终Line
的末端迭代器,而LN将指向文本的末端迭代器,这就是我们将其传递给Text_iterator
构造函数中的Document::end()
中的内容。我们不需要比较或暴露Text_iterator::ln_end
来保留明智的语义。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- 谁可以帮助解释我的代码中出了什么问题.计算C ++中的数组
- 函数以相反的顺序输出输入问题,并改进算法以解释相等的数字
- 有人可以解释一下这个 c++ 代码的作用吗?结构有问题
- 自定义迭代器而不是解释问题
- 有人能解释一下getline和char的问题吗
- 原始二进制树删除与复制问题(改进解释)
- C时间混合问题似乎无法解释,以及如何明智地升级代码库
- 在解释此C++代码时遇到问题
- 谁能解释一下我对欧拉 15 项目的动态编程方法有什么问题
- 有人可以解释一下这个 c++ 代码有什么问题吗?
- C++常见问题解答的不安全宏的解释?
- 关于维基百科上数据段解释的问题
- 请解释这个查找最大指针的过程有什么问题