Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6166641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:22:20+00:00 2026-05-23T22:22:20+00:00

After my recent question , I am trying to implement my own contrived example.

  • 0

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 like

string 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 std;

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:22:21+00:00Added an answer on May 23, 2026 at 10:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have resisted asking this seemingly n00b question, but all my recent effort to
After reading this question , I was reminded of when I was taught Java
I'm sure this question has been asked a thousand times but after an hour
I hope this isn't a stupid question, but after searching around for quite a
I know this is probably an easy question but after reviewing the documentation for
Question title might be meaningless, but I tried. :) Here's my situation: I have
I am currently using JUnit 4.4 and Java 1.6.x. And after a recent code
After Jeph most recent post: http://www.codinghorror.com/blog/archives/001310.html , I thought to myself it would be
I'm a recent semi-convert to Eclipse after 20 years of using vi and gvim.
After reading the Head First Design Patterns book and using a number of other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.