Trying to implement an iterator without an explicit container
After my recent question, I am trying to implement my own contrived example.
I have a basic structure in place but even after reading this, which is probably the best tutorial I've seen, I'm still very confused. I think I should probably convert the Chapter._text into a stream and for the increment operator do something likestring p = "";
string line;
while ( getline(stream, line) ) {
p += line;
}
return *p;
but I'm not sure which of the "boilerplate" typedefs to use and how to put all these things together. Thanks much for your help
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
using namespace st开发者_如何学运维d;
class Paragraph {
public:
string _text;
Paragraph (string text) {
_text = text;
}
};
class Chapter {
public:
string _text;
/* // I guess here I should do something like:
class Iterator : public iterator<input_iterator_tag, Paragraph> {
}
// OR should I be somehow delegating from istream_iterator ? */
Chapter (string txt_file) {
string line;
ifstream infile(txt_file.c_str());
if (!infile.is_open()) {
cout << "Error opening file " << txt_file << endl;
exit(0);
}
while ( getline(infile, line) ) {
_text += (line + "\n");
}
}
};
int main(int argc, char** argv) {
Chapter c(argv[1]);
// want to do something like:
// for (<Paragraph>::iterator pIt = c.begin(); pIt != c.end(); pIt++) {
// Paragraph p(*pIt);
// // Do something interesting with p
// }
return 0;
}
As you weren't planning on a chapter loading at a time (merely a paragraph), and your paragraph is empty, I think this might be best done with a single paragraph_iterator class
class paragraph_iterator :
public std::iterator<std::input_iterator_tag, std::string>
{
std::shared_ptr<std::ifstream> _infile; //current file
std::string _text; //current paragraph
paragraph_iterator(const paragraph_iterator &b); //not defined, so no copy
paragraph_iterator &operator=(const paragraph_iterator &b); //not defined, so no copy
// don't allow copies, because streams aren't copiable.
// make sure to always pass by reference
// unfortunately, this means no stl algorithms either
public:
paragraph_iterator(string txt_file) :_infile(txt_file.c_str()) {
if (!infile.is_open())
throw std::exception("Error opening file ");
std::getline(_infile, _text);
}
paragraph_iterator() {}
paragraph_iterator &operator++() {
std::getline(_infile, _text);
return *this;
}
// normally you'd want operator++(int) as well, but that requires making a copy
// and std::ifstream isn't copiable.
bool operator==(const paragraph_iterator &b) const {
if (_infile.bad() == b._infile.bad())
return true; //all end() and uninitialized iterators equal
// so we can use paragraph_iterator() as end()
return false; //since they all are seperate strings, never equal
}
bool operator!=(const paragraph_iterator &b) const {return !operator==(b);}
const std::string &operator*() const { return _text;}
};
int main() {
paragraph_iterator iter("myfile.txt");
while(iter != paragraph_iterator()) {
// dostuff with *iter
}
}
the stream is encapsulated in the iterator, so that if we have two iterators to the same file, both will get every line. If you have a seperate Chapter class with two iterators, you may run into "threading" problems. This is pretty bare code, and completely untested. I'm sure there's a way to do it with copiable iterators, but far trickier.
In general, an iterator class implementation is closely tied with the data structure it iterates over. Otherwise, we'd just have a few generic iterator classes.
精彩评论