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 936161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:16:08+00:00 2026-05-15T21:16:08+00:00

I fully understand this question has been asked a lot , but I’m asking

  • 0

I fully understand this question has been asked a lot, but I’m asking for a specific variation and my search-foo has given up, as I’ve only found algorithms that append one existing vector to another, but not one returned to from a function.

I have this function that lists all files in a directory:

vector<string> scanDir( const string& dir )

which may call itself internally (for subdirectories).

I need a short way of appending the returned value to the caller’s vector. I have in my mind something like this (but of course it doesn’t exist 🙁 ):

vector<string> fileList;
//...
fileList.append( scanDir(subdirname) );

I fear that storing the return value and inserting it in fileList would bring performance badness. What I mean is this:

vector<string> temp( scanDir(subdirname) );
copy( temp.begin(), temp.end(), back_inserter(fileList) );

Thanks!

PS: I’m not forcing myself to using vector, any other container that performs equally well and can prevent the potential large copy operation is fine by me.

  • 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-15T21:16:09+00:00Added an answer on May 15, 2026 at 9:16 pm

    If you’re in the position to change scanDir, make it a (template) function accepting an output iterator:

    template <class OutIt>
    void scanDir(const std::string& dirname, OutIt it) {
      // ...
      // Scan subdir
      scanDir(subdir, it);
      // ...
    }
    

    You’ll have the additional benefit to be able to fill all sort of data structures like

    std::vector<string> vector;
    scanDir(dir1, std::back_inserter(vector));
    std::set<string> fileset
    scanDir(dir1, std::inserter(fileset, fileset.begin()));
    

    etc.

    EDIT (see comment …)

    For using this function for class member initialization, you could either call it in the constructor as in

    class MyClass {
    private:
      std::vector<string> m_fileList;
    public:
      MyClass(const std::string& dirname) {
        scanDir(dirname, std::back_inserter(m_fileList);
      }
    }
    

    or using a wrapper function

    std::vector<string> scanDir(const std::string& dirname) {
      std::vector<string> result;
      scanDir(dirname, std::back_inserter(result);
      return result;
    }
    
    class MyClass {
    // Same as above..
      MyClass(const std::string& dirname) : m_fileList(scanDir(dirname)) { }
    }
    

    I would prefer the first version for performance (and other) reasons …

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

Sidebar

Related Questions

This question has been asked before but i still don't understand it fully so
I've done some searching, but I've been unable to fully understand how this is
Please not that I fully understand this is a dumb ass idea, but its
I know this has been asked a million times and I did do my
This could be a composite question, as I may not fully understand the issue.
So, this question was just asked on SO: How to handle an "infinite" IEnumerable?
I asked a similar question but I did not provide sufficient details and I
I know that this error has been discussed elsewhere on the web, and this
This has been bugging me for a while and I'm hoping that one of
I saw this question asked about C# I would like an answer for PHP.

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.