【发布时间】:2016-11-25 09:40:21
【问题描述】:
以下代码递归地遍历给定目录并每次都以相同的顺序打印其内容。
是否可以更改目录迭代器以随机方式打印目录内容(即不使用向量存储结果然后随机打印向量内容)?
#include <string>
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
int main(int argc, char** argv)
{
boost::filesystem::path dataPath("/home/test/");
boost::filesystem::recursive_directory_iterator endIterator;
// Traverse the filesystem and retrieve the name of each root object found
if (boost::filesystem::exists(dataPath) && boost::filesystem::is_directory(dataPath)) {
for (static boost::filesystem::recursive_directory_iterator directoryIterator(dataPath); directoryIterator != endIterator;
++directoryIterator) {
if (boost::filesystem::is_regular_file(directoryIterator->status())) {
std::string str = directoryIterator->path().string();
cout << str << endl;
}
}
}
}
【问题讨论】:
-
除非迭代器自己做一些排序,否则顺序将是操作系统为您提供文件条目的顺序,很可能是它们存储在磁盘上的表中的顺序。如果您想要另一个订单,除了中间向量之外别无他法。
标签: c++ boost boost-filesystem