Document.h
#include <vector>
#include <list>
#include <iostream>
using std::list;
using std::istream;
using std::cin;
using std::cout;
typedef std::vector<char> Line;
class Text_Iterator //итератор для класса Document (состоит из 2-х членов)
{
list<Line>::iterator itLine; //строка документа
Line::iterator pos; //позиция в строке
public:
Text_Iterator(list<Line>::iterator ll, Line::iterator pp)
{
itLine = ll; pos = pp;
}
Text_Iterator& operator++()
{
//если pos на конце строки документа, переходим на начало следующей, иначе на след. символ pos
if (++pos == itLine->end())
{
++itLine; pos = itLine->begin();
}
else ++pos;
return *this;
};
Text_Iterator& operator--()
{
//если pos в начале строки, то становимся на конец пред. строки, иначе --pos
if (pos == (itLine->begin()))
{
itLine--; pos = --(itLine->end());
}
else --pos;
return *this;
};
char& operator*() { return *pos; }
bool operator==(const Text_Iterator& arg) const { return (arg.pos == pos); }
bool operator!=(const Text_Iterator& arg) const { return !(*this == arg); } //this не заполнен, почему?
};
//класс текстового документа
struct Document {
list<Line> List;
Document() { List.push_back(Line()); }
Text_Iterator begin() { return Text_Iterator(List.begin(), List.begin()->begin());
}
Text_Iterator end() {
list<Line>::iterator end = List.end();
end--;
Line::iterator end_pos = end->end();
end_pos--;
return Text_Iterator(end, end_pos);
}
//оператор ввода документа
friend istream& operator >> (istream& is, Document& doc)
{
char input;
int i(1);
while (is.get(input))
{
cout << "#" << i++ << ":" << input << "\n";
doc.List.back().push_back(input);
if (input == '\n') //получили символ конца
doc.List.push_back(Line());
}
doc.List.back().push_back('\n');//добавляем чтобы применять back()
return is;
}
};
Когда обхожу контейнер в цикле не корректно проходит !=. Получею assert (MSVS 2015) Main.cpp
int main(int argc, char* argv[])
{
Document txt;
cin >> txt;
for (Text_Iterator it = txt.begin(); it != txt.end(); ++it)
{
cout << *it;
}
_getch();
return 0;
}