By using fs::filesystem I can pre-order traverse Like below code
for (const auto& file : fs::recursive_directory_iterator(paths))
cout << file.path() << endl;
And, I found that recursive_directory_iterator
only supports for pre-order.
Then How Can I use "level-order traversal" in c ?
I think I have to use <vector>
. Any hints?
CodePudding user response:
In loop you can push paths into std::vector
after that loop sort this vector by '/'
character count in paths.
CodePudding user response:
The obvious answer would be to not use a recursive_directory_iterator.
Instead, use a non-recursive iterator with a queue. Start by pushing the root of the directories you want to iterate into the queue.
Then go into a loop:
- pop a directory name from the queue
- process that directory
- when you encounter a directory (that's not a symbolic link), push it into the queue
- repeat until the queue is empty