Algorithm Move Backward Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Move Backward Function

Algorithm Move Backward Function

BLUF: Mastering Algorithm Move Backward Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm Move Backward Function

C++ is renowned for its efficiency. Learn how Algorithm Move Backward Function enables low-level control and high-performance computing in the tutorial below.

The function is employed to shift elements in reverse order, taking in three arguments and relocating the elements within the range [first, last). The process of relocating the elements initiates in reverse sequence, concluding at the 'result' point.

Syntax

Example

template<class BidirectionalIterator1, class BidirectionalIterator2>
BidirectionalIterator2 move_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result);

Parameter

It serves as a bidirectional iterator pointing to the initial element within the range, with the element itself being encompassed by the range.

The # C++ Algorithm Functions move backward iterator points to the final element in the range, excluding the element itself within the specified range.

It functions as a bidirectional iterator pointing to the ultimate destination of the relocated elements.

Return value

The function provides an iterator pointing to the initial element in the series of relocated items.

Example 1

Example

#include <iostream>     
#include <algorithm>   
#include <string>       
int main () 
{
  std::string elem[10] = {"kunal","suraj","shweta","chhavi"};
  std::move_backward (elem,elem+4,elem+5);
  elem[0]="keto";
  std::cout << "elem contains:";
  for (int j=0; j<10; ++j)
    std::cout << " [" << elem[j] << "]";
  std::cout << '\n';
  return 0;
}

Output:

Output

elem contains: [keto] [kunal] [suraj] [shweta] [chhavi] [] [] [] [] []

Example 2

Example

#include<bits/stdc++.h>
int main()
{
	std :: vector <int> u1 {5,9,14,8,18};
	std :: vector <int> u2 {5,5,5,5};
	std :: cout << "u1 contains :";
	for(int j = 0; j < u1.size(); j++)
	std :: cout << " " << u1[j];
	std :: cout << "\n";
	std :: cout << "u2 contains :";
	for(unsigned int j = 0; j < u2.size(); j++)
	std :: cout << " " << u2[j];
	std :: cout << "\n\n";
	std :: move_backward (u2.begin(), u2.begin() + 3, u1.begin() + 3);
	std :: cout << "u1 after applying move_backward function contains:";
	for(unsigned int j = 0; j < u1.size(); j++)
	std :: cout << " " << u1[j];
	std :: cout << "\n";
            return 0;
}

Output:

Output

u1 contains : 5 9 14 8 18
u2 contains : 5 5 5 5

u1 after applying move_backward function contains: 5 5 5 8 18

Complexity

The function's complexity increases linearly from the initial element to the final element.

Data races

Some or all of the container entities are being retrieved.

Exceptions

The function will raise an exception if any of the container items also raises an exception.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience