Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::move_backward

 

std::move_backward is a C++11 STL algorithm to std::move elements from a container to another container. This copying is done in reverse, but the order is preserved.

 

 

 

 

 

 

#include <algorithm>
#include <cassert>
#include <vector>

int main()
{
  ///Create a std::vector to modify
  std::vector<int> v = { 0,1,2,3,4,5,6,7,8,9 };

  ///Create a std::vector to move
  const std::vector<int> w = { 66,77,88 };

  ///Move w to v, w's last element will be the 1-but-last in v
  std::move_backward(w.begin(),w.end(),v.end() - 1);

  assert(v == std::vector<int>( { 0,1,2,3,4,5,66,77,88,9 } ));
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml