Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何加速此目录列表功能?_C++ - Fatal编程技术网

C++ 如何加速此目录列表功能?

C++ 如何加速此目录列表功能?,c++,C++,我用下面的代码列出了一个驱动器的文件。它能工作,但速度很慢。 我怎样才能加快速度 void l(string& foldername, vector<string>& output) { DIR* dir; struct dirent* DirEntry; if ((dir = opendir(foldername.c_str())) != NULL) { while ((DirEntry = readdir(dir)

我用下面的代码列出了一个驱动器的文件。它能工作,但速度很慢。 我怎样才能加快速度

void l(string& foldername, vector<string>& output) {
    DIR* dir;
    struct dirent* DirEntry;

    if ((dir = opendir(foldername.c_str())) != NULL)
    {
        while ((DirEntry = readdir(dir)) != NULL)
        {
            if (strcmp(DirEntry->d_name, ".") && strcmp(DirEntry->d_name, ".."))
            {
                string subfolder(foldername);
                subfolder += "\\";
                subfolder += DirEntry->d_name;
                DIR* subdir;
                if ((subdir = opendir(subfolder.c_str())) != NULL)
                {
                    l(subfolder, output);
                    closedir(subdir);
                }
                else
                {
                    string fullname(foldername);
                    fullname = fullname + '\\' + DirEntry->d_name;
                    output.push_back(fullname);
                }
            }
        }
        closedir(dir);
    }
    else
    {
    }
}
void l(字符串和文件夹名、向量和输出){
DIR*DIR;
结构方向*DirEntry;
if((dir=opendir(foldername.c_str())!=NULL)
{
而((DirEntry=readdir(dir))!=NULL)
{
if(strcmp(DirEntry->d_name,“.”)和strcmp(DirEntry->d_name,“…”)
{
字符串子文件夹(foldername);
子文件夹+=“\\”;
子文件夹+=DirEntry->d_名称;
DIR*subdir;
if((subdir=opendir(subfolder.c_str())!=NULL)
{
l(子文件夹,输出);
closedir(subdir);
}
其他的
{
字符串全名(foldername);
fullname=fullname+'\\'+DirEntry->d\u name;
输出。推回(全名);
}
}
}
closedir(dir);
}
其他的
{
}
}

<代码> > p>如果您使用的是最近的C++标准(>C++ 17),您可以简单地使用<代码> STD::FielSys< /C>。在
std::filesystem::recursive\u directory\u iterator(path)
的帮助下,它的方法更简单,而且您可以用更少的代码行实现您想要的

关于速度,我想它更不是最快的实现,因为它在标准库中。例如,见: