Monday, September 11, 2017

C++11: Algorithms: move_backward

C++11 added a move_backward algorithm. Here is an example:

#include <algorithm>
#include <iostream>

int main()
{
  int myArray[10]  = {1, 2, 3, 8, 9, 0, 0, 0, 0, 0};

  int * inIterBegin = &myArray[0];
  int * inIterEnd   = &myArray[2];
  int * outIterEnd  = &myArray[8];

  int * iter = std::move_backward(inIterBegin, inIterEnd, outIterEnd);
  
  for (auto element : myArray)
    std::cout << element << " ";

  std::cout << std::endl;
  return 0;
}
// Output: 1 2 3 8 9 0 1 2 0 0
// Microsoft compile warning: warning C4996:
//   'std::move_backward::_Unchecked_iterators::_Deprecate': ...
Reference: https://isocpp.org/wiki/faq/cpp11-library-stl#cpp11-algorithms

No comments:

Post a Comment