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

  • SEARCH
  • Home
  • 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 7787825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:47:41+00:00 2026-06-01T20:47:41+00:00

I have written a very simple file managing database that basicly looks like this:

  • 0

I have written a very simple file managing database that basicly looks like this:

class FileDB
{
public:
    FileDB(std::string dir) : rootDir(dir) { }

    void loadFile(std::string filename, File &file) const;
    void saveFile(std::string filename, const File &file) const;

private:
    std::string rootDir;
}

Now I would like to iterate through all files contained in the database like using a std::iterator:

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

I’ve read answers to similar questions, some suggesting to derive std::iterator, some to use std::iterator_traits, 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?

EDIT:
Please don’t consider using boost, my question is of more conceptual nature.

EDIT 2:

The FileDB works like this:

  • rootDir

    • foo1
      • bar1
        • foo1bar1_1.txt
        • foo1bar1_2.txt
      • bar2
        • foo1bar2_1.txt
        • foo1bar2_2.txt
    • foo2

    • fooN

      • barM

        • fooNBarM_x.txt

So basicly, I can find a file by its name.

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 operator== with a string comparison, as the paths should be unique. The iterator returned from fileDB.end() would be an empty string and operator* would call fileDB::loadFile() with its filepath.

My biggest concern is about operator++. 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?

  • 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-06-01T20:47:43+00:00Added an answer on June 1, 2026 at 8:47 pm
    class FileDB
    {
    class iterator;
    
    public:
        FileDB(std::string dir) : m_rootDir(dir) { m_files = getFileTreeList(); }
    
        void loadFile(std::string filename, File &file) const;
        void saveFile(std::string filename, const File &file) const;
    
    
        iterator begin()
        {
          return iterator(m_files.begin(), *this);
        }
        iterator end()
        {
          return iterator(m_files.end(), *this);
        }
    
    private:
        std::list<std::string> getFileTreeList() const;
    
    private:    
        std::string m_rootDir;
        std::list<std::string> m_files;
    }
    
    
    
    class FileDB::iterator
    {
    public:
      iterator(std::list<std::string>::iterator pos, FileDB& owner) : m_pos(pos), m_owner(owner){}
    
      bool operator==(const iterator& rhs) {return m_pos == rhs.m_pos;}
      bool operator!=(const iterator& rhs) {return m_pos != rhs.m_pos;}
      //etc
    
      void operator++() {++m_pos;}
    
      File operator*()
      {
        return openFile(*m_pos); // for example open some file descriptor
      }
    
      ~iterator()
      {
        closeFile(*m_pos); // clean up!
      }
    
    private:
      std::list<std::string>::iterator& m_pos;
      FileDB& m_owner;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a very simple WCF service, that worked fine (code below), then
I have a very simple DLL written in unmanaged C++ that I access from
I have a class library written in .Net that I would like to make
I've written this very simple function to replace a file extension using LINQ in
I have written a very simple test application that creates a websocket and parses
I have written a very simple bash script to help me migrate from dev
I have written a very simple control. C# Visual Studio 2008. Its output should
I have written a very very very simple!! script in php. header redirection not
I have written a very simple function: function editCategory() { $ID = urlencode($_GET['id']); $cname
I have written a very simple application tha accepts batches of telemetry log rows

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.