title: howto parse a path to a vector using c++
body: <p>I need to parse the path part of a URL for a "router" as part of a REST web service. I'm using the PION library for handling HTTP requests. This library does not seem to have any functionality for retrieving parts of the URL path - or so it seems. I cannot find another library that does this. <a href="http://www.w3.org/Library/src/HTParse.c" rel="nofollow">http://www.w3.org/Library/src/HTParse.c</a> does not give parts of the path for example.</p>
  
  <p>Is there a quicker, more robust way of doing this:</p>
  
  <pre><code>std::vector&lt;std::string&gt; parsePath(std::string path)
  {
      std::string delimiter = "/";
      std::string part = "";
      std::size_t firstPos = 0;
      std::size_t secondPos = 0;
      std::vector&lt;std::string&gt; parts;
  
      while (firstPos != std::string::npos)
      {
          firstPos = path.find(delimiter, firstPos);
          secondPos = path.find(delimiter, firstPos + 1);
          part = path.substr(firstPos + 1, (secondPos - 1) - firstPos);
          if (part != "") parts.push_back(part);
          firstPos = secondPos;
      }
  
      return parts;
  }
  </code></pre>