C++ std::rotate_copy with std::vector














































C++ std::rotate_copy with std::vector



Description:
This C++ article is about the rotate_copy() algorithm function with std::vector container.
It is an STL algorithm in <algorithm> header file.
Copies the elements in the range [first,last) to the range beginning at result, but rotating the order of the elements in such a way that the element pointed by start becomes the first element in the resulting range.

rotate_copy() has one definition:
template <class ForwardIterator, class OutputIterator>
  OutputIterator rotate_copy (ForwardIterator first, ForwardIterator start,
                              ForwardIterator last, OutputIterator result);

Return type : An output iterator pointing to the end of the copied range.


Working:The rotate_copy( ) function takes 4 parameters, 'first' , 'last' , 'start' and result.Forward iterators to the initial and final positions of the range to be copy-rotated. Forward iterator pointing to the element within the range [first,last) that is copied to the first position in the range.Output iterator to the initial position of the range where the reversed range is stored.

Implementation: 

#include <iostream>     // std::cout
#include <algorithm>    // std::rotate_copy
#include <vector>       // std::vector

int main () {
  std::vector<int> element {1,2,3,4,5,6,7,8,9};
  std::vector<int> element_size (element.size());

  std::rotate_copy(element.begin(),element.begin()+3,element.end(),element_size.begin());

  // print out content:
  std::cout << "Element contains:";
  for (std::vector<int>::iterator it=element_size.begin(); it!=element_size.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

Output:
Element contains: 4 5 6 7 8 9 1 2 3

More Articles of Mandeep Sheoran:

Name Views Likes
C++ program to insert an element into binary tree 6240 19
C++ program to find an element into binary tree 772 16
C++ std::is_void 572 15
C++ program to find the closest element in binary search tree 916 19
C++ program to replace every element with the least greater element on its right 573 12
C++ program to delete an element into binary tree 784 24
C++ program to find maximum element between two nodes of binary search tree 699 20
C++ std::remove_copy_if with std::vectors 547 11
C++ program to print duplicate elements from the binary search tree 2937 15
C++ program to find depth of the deepest odd level node in binary tree 575 23
C++ program to remove duplicate elements from the binary search tree 1548 20
C++ std::rotate_copy with std::vector 530 14
C++ std::copy_n with std::vector 638 22
C++ std::copy_if with std::vector 1552 18
C++ program to print all the elements of binary search tree 7105 22
C++ std::reverse_copy with std::list 598 18
C++ program to print all the elements of binary tree 1158 18
C++ program to print all full nodes in a binary tree 572 25
C++ program to find sink odd nodes in binary tree 580 15
C++ std::is_copy_assignable 595 22
C++ program to check whether a binary tree is a full binary tree or not using recursion 609 19
C++ std::is_copy_constructible 618 27
C++ program to delete an element into binary search tree 2873 18
C++ std::reverse_copy with std::vector 487 18
C++ std::rotate with std::vector 641 15
C++ program to check for symmetric binary tree using recursion 599 25
C++ program to maximum sum from a tree with adjacent levels not allowed 551 15
C++ std::copy_n with std::list 595 21
C++ program to check if two trees are identical using recursion 545 15
C++ std::copy_n 874 21
C++ std::copy_if with std::list 998 19
C++ program to print the nodes at odd levels of a tree 562 13
C++ program to find lowest common ancestor in a binary tree 689 29
C++ program to find depth of the deepest odd level leaf node 494 13
C++ std::remove_copy_if with std::list 666 20
C++ program to add all greater values to every node in a given binary search tree 638 15

Comments