Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to move an element without removing and re-inserting it into a boost::multi_index_container?
    primarykey
    data
    text
    <p>I'm using a <a href="http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/index.html" rel="nofollow">boost::multi_index_container</a> to provide a random access and a hash-based access to a collection of elements. I wanted to change the random access index of an element, without changing the hash-based index.</p> <p>Here is a piece of code :</p> <pre><code># include &lt;string&gt; # include &lt;boost/multi_index_container.hpp&gt; # include &lt;boost/multi_index/random_access_index.hpp&gt; # include &lt;boost/multi_index/hashed_index.hpp&gt; # include &lt;boost/multi_index/member.hpp&gt; using namespace std ; using namespace boost ; using namespace boost::multi_index ; // class representing my elements class Element { public : Element(const string &amp; new_key) : key(new_key) {} string key ; // the hash-based index in the multi_index_container // ... many stuff skipped private : // ... many stuff skipped } ; typedef multi_index_container&lt; Element, indexed_by&lt; random_access&lt; &gt;, hashed_unique&lt; member&lt;Element, string, &amp;Element::key&gt; &gt; &gt; &gt; ElementContainer ; typedef ElementContainer::nth_index&lt;0&gt;::type::iterator ElementRandomIter ; typedef ElementContainer::nth_index&lt;1&gt;::type::iterator ElementHashedIter ; int main(int, char*[]) { ElementContainer ec ; // insert some elements ec.push_back(Element("Alice")) ; // random-access index = 0 ec.push_back(Element("Bob")) ; // random-access index = 1 ec.push_back(Element("Carl")) ; // random-access index = 2 ec.push_back(Element("Denis")) ; // random-access index = 3 // Here I want to move "Denis" to position 1 // The (bad looking) solution I found involves removing and inserting the element ElementRandomIter it = ec.get&lt;0&gt;().begin() + 3 ; Element e = *(it) ; // store a copy ec.get&lt;0&gt;().erase(it) ; // remove the element it = ec.get&lt;0&gt;().begin() + 1 ; ec.get&lt;0&gt;().insert(it, e) ; // insert the copy // Elements are now in the following order // random-access index 0 : Alice // random-access index 1 : Denis // random-access index 2 : Bob // random-access index 3 : Carl return 0 ; } </code></pre> <p>I know that even if I only used random-access iterators in this example to manipulate elements, hashing occurs behind the scenes at least twice in the <code>multi_index_container</code>, in addition to an object copy, which can be expensive.</p> <p>Is there a method to change the random access index of an element inside a <code>boost::multi_index</code> without requiring an expensive remove-and-insert-while-keeping-a-copy ugliness ?</p> <p>I've searched in the <code>multi_index_container</code> documentation, perhaps I missed something. Thanks for any advice !</p> <p>Note : Sorry for possible English mistakes :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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