37. Finding files in a directory that match a regular expression

Implementing the specified functionality should be straightforward: iterate recursively through all the entries of the specified directory and retain all the entries that are regular files and whose name matches the regular expression. To do that, you should use the following:

  • filesystem::recursive_directory_iterator to iterate through directory entries
  • regex and regex_match() to check whether the filename matches the regular expression
  • copy_if() and back_inserter to copy, at the end of a vector, the directory entries that match a specific criteria.

Such a function may look like this:

namespace fs = std::experimental::filesystem;std::vector<fs::directory_entry> find_files( ...

Get The Modern C++ Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.