Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit</strong></p> <p>Better than before approach without using helper vectors: (<a href="http://www.ideone.com/wRHYv" rel="nofollow">source on ideone</a>):</p> <pre><code>#include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;iostream&gt; template&lt;class Vals&gt; void sortingPermutation(const Vals&amp; values, std::vector&lt;int&gt;&amp; v){ int size = values.size(); v.clear(); v.reserve(size); for(int i=0; i &lt; size; ++i) v.push_back(i); std::sort(v.begin(), v.end(), [&amp;values](int a, int b) -&gt; bool { return values[a] &lt; values[b]; }); } int main() { std::vector&lt;double&gt; values; values.push_back(24); values.push_back(55); values.push_back(22); values.push_back(1); std::vector&lt;int&gt; permutation; sortingPermutation(values, permutation); typedef std::vector&lt;int&gt;::const_iterator I; for (I p = permutation.begin(); p != permutation.end(); ++p) std::cout &lt;&lt; *p &lt;&lt; " "; std::cout &lt;&lt; "\n"; } </code></pre> <p>I am using lambda from C++0x, but it can be replaced with simple functor object:</p> <pre><code>template&lt;class T&gt; struct CmpPairs{ CmpPairs(const std::vector&lt;T&gt; &amp;v): v_(v) {} std::vector&lt;T&gt; v_; bool operator()(int a, int b){ return v_[a] &lt; v_[b]; } }; template&lt;class T&gt; CmpPairs&lt;T&gt; CreateCmpPairs(const std::vector&lt;T&gt; &amp; v) { return CmpPairs&lt;T&gt;(v); } //in sortingPermutation: std::sort(v.begin(), v.end(), CreateCmpPairs(values)); </code></pre> <p>Source of old solution with <code>std::map</code>: <a href="http://www.ideone.com/k1Fxj" rel="nofollow">ideone</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload