title: A method to list all files in directory and sub-directories using boost and c++
body: <p>I have the following code to search all files in a directory and all its sub-directories. the code uses recursion for nested sub-directories.</p>
  
  <pre><code>#include "boost/filesystem.hpp"
  #include &lt;iostream&gt;
  
  using namespace boost::filesystem;
  
  void getDirectoryFiles(path p, vector&lt;string&gt; &amp;files) {
      if (exists(p)) {
          if (is_regular_file(p)) {
              files.push_back(p.string());
              return;
          }
          else if (is_directory(p)) {
              for (directory_entry&amp; x : directory_iterator(p)) {
                  getDirectoryFiles(x.path(), files);
              }
          }
          else {
              cout &lt;&lt; "exists, but not a file of a directory!\n";
              return;
          }
      }
      else {
          cout &lt;&lt; "Path not exists!";
          return;
      }
  };
  </code></pre>
  
  <p>The general idea taken from <a href="http://www.boost.org/doc/libs/1_61_0/libs/filesystem/doc/tutorial.html#Directory-iteration" rel="nofollow">Boost documentation</a> with a twist.
  Any one to optimize this code? </p>