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

Algorithm Move Function

BLUF: Mastering Algorithm Move 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 Function

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

The move function in C++ Algorithm is employed to shift the elements within specified ranges. It takes in three parameters and relocates the elements from the range [first, last) to a new range beginning with 'result'.

Syntax

Example

template<class InputIterator, class OutputIterator> OutputIterator move(InputIterator first, InputIterator last, OutputIterator result);

Parameter

It serves as an input iterator pointing to the initial element within the specified range, with the element itself being encompassed within the range.

The final iterator points to the element just before the end of the specified range, excluding the element itself from the range.

It serves as an output iterator pointing to the original position of the relocated elements.

Return value

The function yields an iterator for the initial item in the series of relocated elements.

Example 1

Example

#include <iostream>     
#include <algorithm>    
#include <utility>      
#include <vector>       
#include <string>       
int main () 
{
  std::vector<std::string> a = {"suraj","aman","vanshika","chhavi"};
  std::vector<std::string> b (4);
  std::cout << "Move function.\n";
  std::move ( a.begin(), a.begin()+4, b.begin() );
  std::cout << "a contains " << a.size() << " elements:";
  std::cout << " (The state of which is valid.)";
  std::cout << '\n';
  std::cout << "b contains " << b.size() << " elements:";
  for (std::string& x: b) std::cout << " [" << x << "]";
  std::cout << '\n';
  std::cout << "Moving the conatiner a...\n";
  a = std::move (b);
  std::cout << "a contains " << a.size() << " elements:";
  for (std::string& x: a) std::cout << " [" << x << "]";
  std::cout << '\n';
  std::cout << "b is in valid state";
  std::cout << '\n';
  return 0;
}

Output:

Output

Move function.
a contains 4 elements: (The state of which is valid.)
b contains 4 elements: [suraj] [aman] [vanshika] [chhavi]
Moving the conatiner a...
a contains 4 elements: [suraj] [aman] [vanshika] [chhavi]
b is in valid state

Example 2

Example

#include<bits/stdc++.h>
int main()
{
	std :: vector <int> u1 {9, 14, 21, 18};
	std :: vector <int> u2 {14, 14, 14, 14};
	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 (u1.begin(), u1.begin() + 4, u2.begin() + 1);
	std :: cout << "u2 contains after move function:";
	for(unsigned int j = 0; j < u2.size(); j++)
		std :: cout << " " << u2[j];
	std :: cout << "\n";
	return 0;
}

Output:

Output

u1 contains : 9 14 21 18
u2 contains : 14 14 14 14

u2 contains after move function: 14 9 14 21

Complexity

The function's complexity increases linearly as it processes elements from the initial one to the final one.

Data races

Some or all of the container entities are retrieved.

Exceptions

The function will raise an exception if any of the container elements 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