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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:40:41+00:00 2026-05-16T11:40:41+00:00

I have a std::vector<std::string> of all the files in a directory: // fileList folder/file1

  • 0

I have a std::vector<std::string> of all the files in a directory:

// fileList
folder/file1
folder/file2
file3
file4.ext

and a std::set<std::string> of filenames and the same for all used folder prefixes:

// set1
file2
file4.ext

// set2
folder

I need to generate the full (relative) paths to the ALL files in set1, but see no way of doing that without iterating over set2 set1.size() times, multiplied by fileList.size()

UPDATE: some clarification:

Expected output for above example:

folder/file2
file4.ext

Proposed (inefficient?) solution, maybe too verbose and with stupid implementation:

// pseudo-code!
vector<string> allpossibleFullPaths( set1.size()*set2.size() );
vector<string> output;
foreach( prefix_in_set2 )
    foreach( filename_in_set1 )
        allpossibleFullpaths.push_back( set2[i] + "/" set1[i] )

foreach( filename_in_fileList )
    files.push_back( find( fileList[i] in allpossibleFullPaths ) );

(fast pseudocode-ish)
This seems very innefficient, is there a better way to make these matches?

Thanks!

PS: better still would be a way to keep track of doubles, so that I can warn the user about that.

  • 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-16T11:40:42+00:00Added an answer on May 16, 2026 at 11:40 am

    One area which you are not clear about is this:

    • Given set1 & set2 as described above, what if fileList had “file4.ext” and “folder\file4.ext”. Would you want both? Or is the list of file in set1 guaranteed to be unique?

    Assuming that you’d want both, pseudo-code:

     foreach(pathname in fileList)
        separate pathname into path & filename.
        if path is not empty, but not in set2, skip to next pathname.
        if filename is in set1, output pathname.
    

    Since a set lookup should be O(1), total complexity is O(2 * fileList.Length)

    If the filenames in set1 are unique, you can count the number of pathnames output, and exit early when set1.Length is reached.

    It may seem counter-intuitive to step through the longest collection, but it also has the slowest lookup, so operations on fileList have to be minimized.

    UPDATE: Here’s the full working C++ code (includes & usings elided)

    void ListFiles()
    {
        vector<string> fileList;
        fileList.push_back("folder/file1");
        fileList.push_back("folder/file2");
        fileList.push_back("file3");
        fileList.push_back("file4.ext");
    
        set<string> set1;
        set1.insert("file2");
        set1.insert("file4.ext");
    
        set<string> set2;
        set2.insert("folder");
    
        for(vector<string>::iterator iter = fileList.begin();
            iter != fileList.end();
            ++iter)
        {
            string pathname = *iter;
            string filename;
            string path;
            size_t pos = pathname.find('/');
            if (pos == string::npos || pos == 0)
                filename = pathname;
            else
            {
                path = pathname.substr(0, pos);
                if (set2.find(path) == set2.end())
                    continue;
                filename = pathname.substr(pos+1);
            }
            if (set1.find(filename) != set1.end())
                cout << pathname << endl;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as
In my current setup, I have a typedef std::function<void (MyClass&, std::vector<std::string>) MyFunction; std::map<std::string, MyFunction>
I have an std::vector that holds all the objects and is passed to them
I have a std::vector with the element as pair and need to find all
I have a vector of strings: std::vector<std::string> fName which holds a list of file
I have several std::vector , all of the same length. I want to sort
So, I have a vector std::vector<std::string> lines. I fill this vector up, and can
I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
I have a std::vector of some class of the form class A{ public: A():i(someNumber){}
I have an std::vector of handle objects. I have to wait on these handle

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.