title: C++ algorithm like python's 'groupby'
body: <p>Are there any C++ transformations which are similar to <a href="http://docs.python.org/library/itertools.html#itertools.groupby"><code>itertools.groupby()</code></a>?</p>
  
  <p>Of course I could easily write my own, but I'd prefer to leverage the idiomatic behavior or compose one from the features provided by the STL or <code>boost</code>.</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;map&gt;
  #include &lt;algorithm&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  struct foo
  {
          int x;
          std::string y;
          float z;
  };
  
  bool lt_by_x(const foo &amp;a, const foo &amp;b)
  {
          return a.x &lt; b.x;
  }
  
  void list_by_x(const std::vector&lt;foo&gt; &amp;foos, std::map&lt;int, std::vector&lt;foo&gt; &gt; &amp;foos_by_x)
  {
          /* ideas..? */
  }
  
  int main(int argc, const char *argv[])
  {
          std::vector&lt;foo&gt; foos;
          std::map&lt;int, std::vector&lt;foo&gt; &gt; foos_by_x;
  
          std::vector&lt;foo&gt; sorted_foos;
          std::sort(foos.begin(), foos.end(), lt_by_x);
          list_by_x(sorted_foos, foos_by_x);
  
          return EXIT_SUCCESS;
  }
  </code></pre>