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....

有帮助吗?

解决方案

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
} 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top