How to Reverse Vector Elements Using STL Utilities in C++

Jinku Hu Feb 02, 2024
  1. Use the std::reverse Algorithm to Reverse Vector Elements in C++
  2. Use the std::shuffle Algorithm to Reorder Vector Elements Randomly in C++
  3. Use the std::rotate Algorithm to Rotate Vector Elements in C++
How to Reverse Vector Elements Using STL Utilities in C++

This article will introduce how to reverse vector elements using STL utilities in C++.

Use the std::reverse Algorithm to Reverse Vector Elements in C++

std::reverse is part of the STL algorithms and can be used to reverse the order of elements in any given range. The std::reverse algorithm internally swaps two elements starting from the first and the last pair. std::reverse takes two arguments that represent iterators of the given range. We generate random integers as a vector object in the following example, which is reversed using the std::reverse algorithm and output the results to the cout stream.

#include <iomanip>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>

using std::cout;
using std::endl;
using std::left;
using std::setw;

void generateNumbers(std::vector<int> &arr, size_t &width) {
  std::srand(std::time(nullptr));
  for (size_t i = 0; i < width; i++) {
    arr.push_back(std::rand() % 100);
  }
}

int main() {
  size_t width = 10;
  std::vector<int> arr;
  arr.reserve(width);

  generateNumbers(arr, width);

  cout << left << setw(10) << "arr: ";
  copy(arr.begin(), arr.end(), std::ostream_iterator<int>(cout, "; "));
  cout << endl;

  std::reverse(arr.begin(), arr.end());

  cout << left << setw(10) << "arr: ";
  copy(arr.begin(), arr.end(), std::ostream_iterator<int>(cout, "; "));
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

arr:      78; 56; 63; 59; 16; 7; 54; 98; 87; 92;
arr:      92; 87; 98; 54; 7; 16; 59; 63; 56; 78;

Use the std::shuffle Algorithm to Reorder Vector Elements Randomly in C++

std::shuffle can be used to randomly reorder elements in the range such that each permutation of elements has an equal probability. The function takes at least two arguments which denote starting and ending iterators of the range. Optionally, std::shuffle can take the third argument representing the random number generator function. In this case, we utilized mersenne_twister_engine, which is provided under the <random> header.

#include <iomanip>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>

using std::cout;
using std::endl;
using std::left;
using std::setw;

void generateNumbers(std::vector<int> &arr, size_t &width) {
  std::srand(std::time(nullptr));
  for (size_t i = 0; i < width; i++) {
    arr.push_back(std::rand() % 100);
  }
}

int main() {
  size_t width = 10;
  std::vector<int> arr;
  arr.reserve(width);

  generateNumbers(arr, width);

  cout << left << setw(10) << "arr: ";
  copy(arr.begin(), arr.end(), std::ostream_iterator<int>(cout, "; "));
  cout << endl;

  std::shuffle(arr.begin(), arr.end(), std::mt19937(std::random_device()()));

  cout << left << setw(10) << "arr: ";
  copy(arr.begin(), arr.end(), std::ostream_iterator<int>(cout, "; "));
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

arr:      40; 77; 74; 41; 79; 21; 81; 98; 13; 90;
arr:      79; 41; 90; 77; 21; 81; 98; 74; 13; 40;

Use the std::rotate Algorithm to Rotate Vector Elements in C++

Another useful function included in the STL algorithms is - std::rotate. The function shifts elements left and wraps around the elements that are moved outside of the vector boundary. std::rotate takes three arguments of type ForwardIt iterators and performs the rotation so that the element pointed by the second argument is moved to the first position of the newly generated list.

#include <iomanip>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>

using std::cout;
using std::endl;
using std::left;
using std::setw;

void generateNumbers(std::vector<int> &arr, size_t &width) {
  std::srand(std::time(nullptr));
  for (size_t i = 0; i < width; i++) {
    arr.push_back(std::rand() % 100);
  }
}

int main() {
  size_t width = 10;
  std::vector<int> arr;
  arr.reserve(width);

  generateNumbers(arr, width);

  cout << left << setw(10) << "arr: ";
  copy(arr.begin(), arr.end(), std::ostream_iterator<int>(cout, "; "));
  cout << endl;

  std::rotate(arr.begin(), arr.begin() + (width / 2), arr.begin() + width);

  cout << left << setw(10) << "arr: ";
  copy(arr.begin(), arr.end(), std::ostream_iterator<int>(cout, "; "));
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

arr:      75; 16; 79; 62; 53; 5; 77; 50; 31; 54;
arr:      5; 77; 50; 31; 54; 75; 16; 79; 62; 53;
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Vector