TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm move_backward() function

C++ algorithm move_backward() function tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm Functions move backward ()

The function is used for moving of elements in the backward order, it accepts three arguments and then moves the elements belonging to the range [first,last). The moving of elements begins in the reverse order with termination point at 'result'.

Syntax

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

Parameter

first: It is a bidirectional iterator to the first element of the range, where the element itself is included in the range.

last: It is a bidirectional iterator to the last element of the range, where the element itself is not included in the range.

result: It is a bidirectional iterator to the final position of moved elements.

Return value

The function returns an iterator the first element to the sequence of moved ones.

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 complexity of the function is linear starting from the first element to the last one.

Data races

Some or all of the container objects are accessed.

Exceptions

The function throws an exception if any of the container elements throws one.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf