sorting - Sort by proxy (or: sort one container by the contents of another) in C++ -


I have a set of data that is divided into two arrays (let's call them data And key ). That is, with the index i for any item, I can access the data of that item with the data [i] and for that item the key keys [i] . I can not change this structure (i.e., to press the keys and data in an array), because I have to pass the data array for a library function that is expected of a particular data layout Does.

key

Create a vector of objects, which will be indexed for two arrays. Operator & lt; Define for that object to compare based on keys [index] . Sort the vector when you are finished, leave that vector and keep your original objects in the order defined by those proxy objects:

  // Warning: Unwanted code. Struct sort_proxy {size_t i; The Bull Operator & lt; (Sort_proxy const and other) cost {return bracket [i] & lt; Key [other]; }}; // ... key_size = number of keys / data items std :: vector & lt; Sort_proxy & gt; Proxy (key_size); For (i = 0; i & lt; keys_size; i ++) proxy [i] .i = i; Std :: sort (proxies.begin (), proxies.end ()); Excluded as an exercise for // in-place reading reader. For :-) (int i = 0; i  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -