C++ std::rotate with std::vector

C++ std::rotate with std::vector

This article is based on C++11.

This C++ article is about the rotate () algorithm function with std::vector container.
It is an STL algorithm in <algorithm> header file.
Rotates the order of the elements in the range [first,last), in such a way that the element pointed by start becomes the new first element.

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

Return type : An iterator pointing to the element that now contains the value previously pointed by first.

Working: The rotate( ) function takes 3 parameters, 'first' & 'last' and 'start' .Forward iterators to the initial and final positions of the sequence to be rotated left.Forward iterator pointing to the element within the range [first,last) that is moved to the first position in the range.


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

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

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

  return 0;

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 7322 19
C++ program to find an element into binary tree 843 16
C++ std::is_void 626 15
C++ program to find the closest element in binary search tree 1066 19
C++ program to replace every element with the least greater element on its right 626 12
C++ program to delete an element into binary tree 854 24
C++ program to find maximum element between two nodes of binary search tree 750 20
C++ std::remove_copy_if with std::vectors 617 11
C++ program to print duplicate elements from the binary search tree 3160 15
C++ program to find depth of the deepest odd level node in binary tree 630 23
C++ program to remove duplicate elements from the binary search tree 1824 20
C++ std::rotate_copy with std::vector 602 14
C++ std::copy_n with std::vector 689 22
C++ std::copy_if with std::vector 1693 18
C++ program to print all the elements of binary search tree 8405 22
C++ std::reverse_copy with std::list 654 18
C++ program to print all the elements of binary tree 1400 18
C++ program to print all full nodes in a binary tree 619 25
C++ program to find sink odd nodes in binary tree 633 15
C++ std::is_copy_assignable 657 22
C++ program to check whether a binary tree is a full binary tree or not using recursion 694 19
C++ std::is_copy_constructible 676 27
C++ program to delete an element into binary search tree 3443 18
C++ std::reverse_copy with std::vector 536 18
C++ std::rotate with std::vector 848 15
C++ program to check for symmetric binary tree using recursion 653 25
C++ program to maximum sum from a tree with adjacent levels not allowed 616 15
C++ std::copy_n with std::list 665 21
C++ program to check if two trees are identical using recursion 597 15
C++ std::copy_n 988 21
C++ std::copy_if with std::list 1193 19
C++ program to print the nodes at odd levels of a tree 618 13
C++ program to find lowest common ancestor in a binary tree 735 29
C++ program to find depth of the deepest odd level leaf node 538 13
C++ std::remove_copy_if with std::list 739 20
C++ program to add all greater values to every node in a given binary search tree 709 15