Monday, September 11, 2017

C++11: Algorithms: copy_n

C++11 added a copy_n 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[5];
  int * outIterBegin  = &myArray[5];

  int * iter = std::copy_n(inIterBegin, 5, outIterBegin);
  
  for (auto element : myArray)
    std::cout << element << " ";

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

No comments:

Post a Comment