【问题标题】:Can the order of files be changed when iterating a directory using boost::filesystem?使用 boost::filesystem 迭代目录时可以更改文件的顺序吗?
【发布时间】:2018-02-14 10:10:19
【问题描述】:

我已经使用 boost 的 directory_iterator 继承了一些代码:

for (directory_iterator fileIt{currDir}; fileIt != directory_iterator{}; ++fileIt) {
  // Do stuff with files
}

我想按特定顺序处理文件(简单的字母排序就可以了)。有什么方法可以实现这一点,即强制迭代器根据某种排序而不是默认排序给我文件?

【问题讨论】:

  • 将其放入容器中,对其进行分类并使用它有用吗?
  • @GauravSehgal 如果没有更好的方法,那是我的后备计划...... :)

标签: c++ file iterator boost-filesystem


【解决方案1】:

您需要自己对其进行排序,因为通过取消引用 directory_iterator 的连续增量获得的目录条目的顺序是未指定的

std::vector<boost::filesystem::path> paths(
      boost::filesystem::directory_iterator{"."}
    , boost::filesystem::directory_iterator{}
    );
std::sort(paths.begin(), paths.end());
for(auto const& path : paths)
    std::cout << path << '\n';

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2010-11-18
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
相关资源
最近更新 更多