title: Is there an easier way to pop off a directory from boost::filesystem::path?
body: <p>I have a relative path (e.g. "foo/bar/baz/quux.xml") and I want to pop a directory off so that I will have the subdirectory + file (e.g. "bar/baz/quux.xml").</p>
  
  <p>You can do this with path iterators, but I was hoping there was something I was missing from the documentation or something more elegant.  Below is the code that I used.</p>
  
  <pre><code>#include &lt;boost/filesystem/path.hpp&gt;
  #include &lt;boost/filesystem/operations.hpp&gt;
  #include &lt;boost/filesystem/convenience.hpp&gt;
  #include &lt;boost/filesystem/exception.hpp&gt;
  
  #include &lt;boost/assign.hpp&gt;
  
  boost::filesystem::path pop_directory(const boost::filesystem::path&amp; path)
  {
      list&lt;string&gt; parts;
      copy(path.begin(), path.end(), back_inserter(parts));
  
      if (parts.size() &lt; 2)
      {
          return path;
      }
      else
      {
          boost::filesystem::path pathSub;
          for (list&lt;string&gt;::iterator it = ++parts.begin(); it != parts.end(); ++it)
          {
              pathSub /= *it;
          }
          return pathSub;
      }
  }
  
  int main(int argc, char* argv)
  {
    list&lt;string&gt; test = boost::assign::list_of("foo/bar/baz/quux.xml")
    ("quux.xml")("foo/bar.xml")("./foo/bar.xml");
    for (list&lt;string&gt;::iterator i = test.begin(); i != test.end(); ++i)
    {
      boost::filesystem::path p(*i);
      cout &lt;&lt; "Input: " &lt;&lt; p.native_file_string() &lt;&lt; endl;
  
      boost::filesystem::path p2(pop_directory(p));
  
      cout &lt;&lt; "Subdir Path: " &lt;&lt; p2.native_file_string() &lt;&lt; endl;
    }
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Input: foo/bar/baz/quux.xml 
  Subdir Path: bar/baz/quux.xml
  Input: quux.xml
  Subdir Path: quux.xml 
  Input: foo/bar.xml 
  Subdir Path: bar.xml
  Input: ./foo/bar.xml 
  Subdir Path: foo/bar.xml
  </code></pre>
  
  <p>What I was hoping for was something like:</p>
  
  <pre><code>boost::filesystem::path p1(someString);
  boost::filesystem::path p2(p2.pop());
  </code></pre>
  
  <p>If you look at some <a href="http://codepad.org/2h0V1HE0" rel="nofollow noreferrer">test code on codepad.org</a>, I have tried branch_path (returns "foo/bar/baz") and relative_path (returns "foo/bar/baz/quux.xml").</p>