如何移动元素而不删除并重新插入到boost :: multi_index_container中?

ove*_*der 5 c++ boost multi-index

我正在使用boost :: multi_index_container来提供随机访问和对元素集合的基于散列的访问.我想更改元素的随机访问索引,而不更改基于哈希的索引.

这是一段代码:

# include <string>
# include <boost/multi_index_container.hpp>
# include <boost/multi_index/random_access_index.hpp>
# include <boost/multi_index/hashed_index.hpp>
# include <boost/multi_index/member.hpp>

using namespace std ;
using namespace boost ;
using namespace boost::multi_index ;

// class representing my elements
class Element
{
    public :
      Element(const string & 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<
            Element,
            indexed_by<
                random_access< >,
                hashed_unique<
                    member<Element, string, &Element::key>
                >
            >    
        > ElementContainer ;

typedef ElementContainer::nth_index<0>::type::iterator ElementRandomIter ;
typedef ElementContainer::nth_index<1>::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<0>().begin() + 3 ;
    Element e = *(it) ;                    // store a copy
    ec.get<0>().erase(it) ;                // remove the element
    it = ec.get<0>().begin() + 1 ;
    ec.get<0>().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 ;
}
Run Code Online (Sandbox Code Playgroud)

我知道即使我在这个例子中只使用随机访问迭代器来操作元素,multi_index_container除了对象副本之外,在场景后面至少两次发生散列,这可能很昂贵.

有没有一种方法可以更改内部元素的随机访问索引,boost::multi_index而无需昂贵的删除和插入,同时保持复制丑陋?

我在multi_index_container文档中搜索过,也许我错过了一些东西.谢谢你的建议!

注意:抱歉可能出现英语错误:)