title: How to realize a custom implementation of a std-like iterator?
body: <p>I have written a very simple file managing database that basicly looks like this:</p>
  
  <pre><code>class FileDB
  {
  public:
      FileDB(std::string dir) : rootDir(dir) { }
  
      void loadFile(std::string filename, File &amp;file) const;
      void saveFile(std::string filename, const File &amp;file) const;
  
  private:
      std::string rootDir;
  }
  </code></pre>
  
  <p>Now I would like to iterate through all files contained in the database like using a <code>std::iterator</code>:</p>
  
  <pre><code>void iterateFiles()
  {
      FileDB filedb("C:\\MyFiles");
  
      for (FileDB::iterator file_it = filedb.begin(); file_it != filedb.end(); ++file_it)
      {
          File f = *file_it;
          // do something with file
      }
  }
  </code></pre>
  
  <p>I've read answers to similar questions, some suggesting to derive <code>std::iterator</code>, some to use <code>std::iterator_traits</code>, but I don't really understand how to do that. What can possibly go wrong when trying to implement a custom iterator? And what's a simple yet elegant way to do it?</p>
  
  <p><strong>EDIT:</strong>
  Please don't consider using boost, my question is of more conceptual nature.</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>The FileDB works like this:</p>
  
  <ul>
  <li><p>rootDir</p>
  
  <ul>
  <li>foo1
  <ul>
  <li>bar1
  <ul>
  <li>foo1bar1_1.txt</li>
  <li>foo1bar1_2.txt</li>
  </ul></li>
  <li>bar2
  <ul>
  <li>foo1bar2_1.txt</li>
  <li>foo1bar2_2.txt</li>
  </ul></li>
  </ul></li>
  <li><p>foo2</p></li>
  <li><p>fooN</p>
  
  <ul>
  <li><p>barM</p>
  
  <ul>
  <li>fooNBarM_x.txt</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>So basicly, I can find a file by its name.</p>
  
  <p>As my container is not in memory, I don't have pointers to its data. So my idea was to store the file's path in the iterator. This way, I can implement <code>operator==</code> with a string comparison, as the paths should be unique. The iterator returned from <code>fileDB.end()</code> would be an empty string and <code>operator*</code> would call <code>fileDB::loadFile()</code> with its filepath.</p>
  
  <p>My biggest concern is about <code>operator++</code>. Having the filename, I can find out the containing directory and search for the next file, but this is really ineffective. Any ideas on how to do that? Or am I completely wrong with my whole concept?</p>