문제

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