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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:38:26+00:00 2026-06-13T22:38:26+00:00

I haven’t coded this bit up yet, because I’m not sure of which is

  • 0

I haven’t coded this bit up yet, because I’m not sure of which is the best method to tackle this.

For starters, what the program does now is simply put the names of all the files in the same directory as the program into an array of strings and then print that array out.

What I want to do is sort these by file extension. There will be a list of particular extensions for the user to choose from, after which all files with that extension in the folder will be returned to the user.

I’m just not sure how to go about that. The first thing that comes to mind is to iterate through the vector and compare each string to another string with the desired extension, and if there is match then push that string into another vector that is specific for that file extension. There are only 5 extensions I’m looking for so it’s not like I would have to make a whole ton of new vectors for each extension.

Alternativley I thought it might also make sense to never populate the original vector, and take the users request first and then iterate through the files and push all files with matching extensions into a specific vector. Once done if they choose another option the vector will simply be cleared and re-populated with the new file names.

Any tips on how to go about actually doing the comparison, I’m not that good with c++ syntax, also would it be wise to use a different type of container?

Thanks a lot for any and all advice you guys are willing to throw my way, it’s greatly appreciated!

#include <iostream>
#include <filesystem>
#include <vector>
using namespace std;
using namespace std::tr2::sys;


void scan( path f, unsigned i = 0 )
{
string indent(i,'\t');
cout << indent << "Folder = " << system_complete(f) << endl;
directory_iterator d( f );
directory_iterator e;

vector<string>::iterator it1;

std::vector<string> fileNames;


for( ; d != e; ++d )
{
    fileNames.push_back(d->path());

    //print out conents without use of an array
    /*cout << indent << 
        d->path() << (is_directory( d->status() ) ? " [dir]":"") <<
        endl;*/

    //if I want to go into subdirectories
    /*if( is_directory( d->status() ) )
        scan( f / d->path(), i + 1 );*/
}

for(it1 = fileNames.begin(); it1 != fileNames.end(); it1++)
{
 cout << *it1 << endl;
}



}


int main()
{
    path folder = "..";

    cout << folder << (is_directory( folder ) ? " [dir]":"") << endl;

    scan( folder );
}
  • 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-13T22:38:27+00:00Added an answer on June 13, 2026 at 10:38 pm

    You don’t mean ‘sort’, you mean ‘filter’. Sort means something else entirely.

    Your second option seems the best, why do the extra work with two vectors?

    As for the comparison, the difficulty is that the thing you are looking for is at the end of the string, and most searching functions operate from the start of the string. But there is a handy thing in C++ called a reverse iterator which scans a string backwards from the end, not forwards from the start. You call rbegin() and rend() to get a string’s reverse iterators. Here’s a comparison function using reverse iterators.

    #include <algorithm>
    #include <string>
    
    // return true if file ends with ext, false otherwise
    bool ends_with(const std::string& file, const std::string& ext)
    {
        return file.size() >= ext.size() && // file must be at least as long as ext
            // check strings are equal starting at the end
            std::equal(ext.rbegin(), ext.rend(), file.rbegin());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I haven't written any code yet because i don't know where to start. I
I haven't found a definite answer to this yet. Lots of apps let you
haven't used regex replaces much and am not sure if how I have done
Haven't seen many Geneva related questions yet, I have posted this question in the
Haven't found a solution to this problem yet, have tried some things... I have
Haven't seen this feature anywhere else. I know that the 32nd bit is used
I haven't made any thorough analysis of which implementation of PHP is the best
I haven't used the STL much before, but I started to on this huffman
I haven't used ARC yet other than to deal with it when it forces
I haven't been able to get this working and all of the sample code

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.