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
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
#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:
elem contains: [keto] [kunal] [suraj] [shweta] [chhavi] [] [] [] [] []
Example 2
#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:
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.