E
E
Egorithm2020-07-09 20:02:47
C++ / C#
Egorithm, 2020-07-09 20:02:47

STL. filesystem. Is there a way to reverse iterate over a directory?

Please do not confuse with recursive iteration. I need to somehow get fs::directory_entryfrom the end to the beginning. There are no methods operator--()or decrement()the class fs::directory_iterator, there are no traits for the constructor either. The type class is fs::reverse_directory_iteratormissing. So is there a way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egorithm, 2020-07-09
@EgoRusMarch

In general, the most normal way is to create std::set<fs::path>and fill it with content, and then use a reverse iterator std::set.

std::set<fs::path> file_paths;

for (const auto& dir_entry : fs::directory_iterator{"path/to/directory"})
    file_paths.insert(dir_entry.path());

auto iter = file_paths.rbegin();
auto end = file_paths.rend();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question