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)
的帮助下,它的方法更简单,而且您可以用更少的代码行实现您想要的
关于速度,我想它更不是最快的实现,因为它在标准库中。例如,见: