How to use std::move() in C++ to move numbers from one array to another

How to use std::move() in C++ to move numbers from one array to another:

std::move() is defined in algorithm C++ STL library. We can use this method to move items in a range from one array to another array in C++. In this post, we will learn how to use move to move items from one array to a different array with example.

Definition of std::move():

move() is defined as below:

OutputIterator move(InputIterator first, InputIterator last, OutputIterator result)

It moves all elements pointed by first to last to the result. It includes the element pointed by first but not the element pointed by last. result is the destination position.

Return value is an iterator to the end of the destination range where elements are moved.

Example of std::move():

Let me show you an example of std::move and how it works:


using namespace std;

int main(){
  int firstArr[] = {1,2,3,4,5,6,7};
  int secondArr[] = {8,9,10,11,12,13,14};

  move(firstArr, firstArr + 4, secondArr + 1);

  int size = sizeof(secondArr)/sizeof(secondArr[0]);

  for(int i = 0; i< size; i++){
    cout<<secondArr[i]<< " ";


8 1 2 3 4 13 14


  • Here, firstArr and secondArr are two given arrays.
  • We are calling move from the start of firstArr to 4, i.e. it will include all element from the index 0 to 3 of firstArr. The last parameter secondArr + 1 indicates that we are copying from the index 1 of the secondArr array.
  • size holds the size of the secondArr.
  • Finally, using a for loop, we are printing the content of the secondArr.

As you can see here, this method copies 1,2,3,4 from the firstArr to secondArr from index 1 to 4.

You might also like: