Question

What does relocate() mean in boost multi-index container?

I have read the manual from boost documentations, but I want to see a simple example and see the difference of using and not using the relocate function. The examples on the web are not simple though....

Was it helpful?

Solution

It merely relocates (moves) item(s) in a sequenced index:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <iostream>

using namespace boost::multi_index;

typedef multi_index_container<
  int,
  indexed_by<sequenced<> > 
> Ints;


int main() 
{ 
  Ints ints;
  ints.insert(ints.end(), 1);
  ints.insert(ints.end(), 2);
  ints.insert(ints.end(), 3);
  ints.insert(ints.end(), 4);
  std::for_each (ints.begin(), ints.end(), [&](int i) { std::cout << i << std::endl; }); // 1, 2, 3, 4

  auto i = find(ints.begin(), ints.end(), 2);
  ints.relocate(ints.end(), i);
  std::for_each (ints.begin(), ints.end(), [&](int i) { std::cout << i << std::endl; }); // 1, 3, 4, 2
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top