title: How to pass user data to compare function of std::sort?
body: <p>Like <code>qsort()</code>, it seems that C++ <code>std::sort()</code> does not allow to pass user data to the sort function. </p>
  
  <p>For example:
  An array of structure like <code>struct A { int version; int index; } array[100]</code> has to be sorted in order, but using this array <code>struct B { int value; } key[100]</code> as the sort key. <code>struct A::index</code> indexes array <code>key</code>.</p>
  
  <p>Here's a non-working sort function. It needs to have a pointer to the <code>key</code> array somehow:</p>
  
  <pre><code>bool comp(struct A *a1, struct A *a2) {
      return key[a1-&gt;index].value &lt; key[a2-&gt;index].value;
  }
  </code></pre>
  
  <p>How to achieve that using C++? How to pass non-global user data like <code>key</code> to a sort function?</p>
  
  <p>I tried to pass an object instance as the <code>std::sort</code> comp, but it seems only <code>qsort()</code>-like functions are allowed.</p>
  
  <p>(In GNU C, a nested compare function could be used to use scoped variables, but GNU C++ does not offer nested functions).</p>