c++ - 尝试在没有显式容器的情况下实现迭代器

标签 c++ stl iterator

在我最近 question 之后,我正在尝试实现我自己设计的示例。
我有一个基本的结构,但即使在阅读之后this ,这可能是我见过的最好的教程,我仍然很困惑。我想我应该将 Chapter._text 转换成一个流,并为增量运算符做类似的事情

string p = "";
string line;
while ( getline(stream, line) ) {
    p += line;
}
return *p;

但我不确定要使用哪个“样板”typedef 以及如何将所有这些东西放在一起。非常感谢您的帮助

#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;
}

最佳答案

因为您没有计划一次加载一个章节(只是一个段落),而且您的段落是空的,我认为这最好用一个 paragraph_iterator 类来完成

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
    }

}

流被封装在迭代器中,因此如果我们有两个指向同一个文件的迭代器,它们都将获取每一行。如果您有一个带有两个迭代器的单独的 Chapter 类,您可能会遇到“线程”问题。这是非常简单的代码,并且完全未经测试。我确信有一种方法可以使用可复制的迭代器来做到这一点,但要复杂得多。

一般来说,迭代器类的实现与其迭代的数据结构密切相关。否则,我们只会有一些通用的迭代器类。

关于c++ - 尝试在没有显式容器的情况下实现迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6366139/

相关文章:

c++ - 如何将重写的虚函数的指针发送到基类?

c++ - less 或 less_equal 使用 set

c++ - gdb catch 抛出然后忽略异常

c++ - 在 header 中声明和初始化 vector (还有其他 STL 容器)

c++ - STL 排序 - 严格弱排序

c++ - 跑完定制迭代器的末尾

jsp - 如何使用 JSTL <c :forEach> with Struts2 <s:url>?

class - 如何将迭代器关联到 OCaml 中的集合

c++ - 如何在通过 nvcc 传递到 VS2015 的文件上启用 OMP?

c# - 将结构中的结构从 C# 编码到 C++