Hi all,

Here's a code using a list of vectors to read a text terminated by ctrl+z and then print it all in the same order. The problem is that I don't know why is the function Text_iterator& Text_iterator:: operator++() correct and especially how can the condition if (pos == ln->end()) be true!?

Code:
#include <iostream>
#include <vector>
#include <list>

using Line = std::vector<char>;
using listIter = std::list<Line>::iterator;
using lineIter = Line::iterator;

struct Text_iterator {

	listIter ln;
	lineIter pos; 

	Text_iterator(listIter ll, lineIter pp)
		: ln{ ll }, 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++()
{
	++pos;    
   if (pos == ln->end()) {
		++ln;  // "ln" pointing to the next vector/line
		pos = ln->begin();
	} 
	return *this;
}

//************************

class Document {
public:
	std::list<Line> lines;

	Document() { lines.push_back(Line{}); } 

	Text_iterator begin()
	{
		return Text_iterator(lines.begin(), lines.begin()->begin());
	}

	Text_iterator end()
	{
		listIter last = lines.end();
		--last;  // We know that the document is not empty
		return Text_iterator(last, last->end());
	}
};

//******************************************************

std::istream& operator>>(std::istream& is, Document& d)
{
	char ch;
	while (is.get(ch)) 
	{
		d.lines.back().push_back(ch); 
		if (ch == '\n')
			d.lines.push_back(Line{}); // An empty vector of char with uninitialized values
	}

	if (d.lines.back().size()) d.lines.push_back(Line{});  // Add final empty line
	return is;
}

//******************************

int main()
{
	Document d;

	std::cout << "Please enter characters for your list. Type ^z at the end.\n";
	std::cin >> d;

	for (auto p = d.begin(); p != d.end(); ++p)
		std::cout << *p;

	system("pause");
	return 0;
}