# Different methods to copy in C++ STL - std::copy(), copy_n(), copy_if(), copy_backwards()

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

As the method name suggests copy() method is used to copy the data through various methods available in C++ STL. All the methods differ in functionalities and the parameters. These methods are available in <algorithm> header file. Let’s discuss each method and their functionalities.

## Copy(start_i1, end_i1, start_i2)

This method is used to copy the data from one iterator to another iterator within specified range where both the start and end elements of an iterator are inclusive. It takes three types of arguments i.e. −

• Start_i1 − It will point to the initial element of the iterator, let’s say, i_1 from where the element will be copied to another iterator, let’s say, i_2.

• End_i1 − It will point to the end element of the iterator, let’s say, i_1 till where the element will be copied to another iterator, let’s say, i_2.

• Start_i2 − It will point to the initial location of an iterator at where the elements will be copied i.e. i_2.

Return Value − It will return an iterator pointing to the end of the destination iterator i.e. i_2 till where the elements are copied.

### Example

Live Demo

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
//creating vector v1
vector<int> vec_1 = { 10, 20, 30, 40, 50 };
//declaring empty vector of size
vector<int> vec_2(6);
// using copy() function to copy in vector 2
copy(vec_1.begin(), vec_1.begin()+4, vec_2.begin());
//print new vector
cout<<"Elements in vector v2 copied from v1: ";
for(int i=0; i<4; i++){
cout<<vec_2[i] << " ";
}
}

### Output

Output of this code will be −

Elements in vector v2 copied from v1: 10 20 30 40

## copy_n(start_i1, total, start_i2)

This method is also used to copy the data from one iterator to another iterator but it tells the compiler that in total how many elements need to be copied starting from a given location. It takes three types of arguments i.e.−

• Start_i1 − It will point to the initial element of the iterator, let’s say, i_1 from where the element will be copied to another iterator, let’s say, i_2.

• Total − It describes how many elements will be copied starting from the location specified by start_i1. It can take both positive and negative integer but won’t perform any operation if it is a negative value.

• Start_i2 − It will point to the initial location of an iterator at where the elements will be copied i.e. i_2.

Return Value − It will return an iterator pointing to the end of the destination iterator i.e. i_2 till where the elements are copied.

### Example

Live Demo

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
//creating vector v1
vector<int> vec_1 = { 10, 20, 30, 40, 50 };
//declaring empty vector of size
vector<int> vec_2(6);
// using copy_n() function to copy in vector 2
copy_n(vec_1.begin(), 4, vec_2.begin());
//print new vector
cout<<"Elements in vector v2 copied from v1: ";
for(int i=0; i<4; i++){
cout<<vec_2[i] << " ";
}
}

### Output

Output of this code will be −

Elements in vector v2 copied from v1: 10 20 30 40

## copy_if(start_i1, end_i1, start_i2, Boolean function)

This method is used to copy the data from one iterator to another iterator within specified range based upon the condition applied to the range which will be defined in the fourth argument passed to this function. It takes four types of arguments i.e.−

• Start_i1 − It will point to the initial element of the iterator, let’s say, i_1 from where the element will be copied to another iterator, let’s say, i_2.

• End_i1 − It will point to the end element of the iterator, let’s say, i_1 till where the element will be copied to another iterator, let’s say, i_2.

• Start_i2 &minbus; It will point to the initial location of an iterator at where the elements will be copied i.e. i_2.

• Boolean function − In this function, we will pass the condition we want to impose to the range. Since the return type of this function is Boolean it will return true/false and based upon the return value the range elements will be displayed.

Return Value − It will return an iterator pointing to the end of the destination iterator i.e. i_2 till where the elements are copied.

### Example

Live Demo

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
//creating vector v1
vector<int> vec_1 = { 10, 21, 30, 40, 57 };
//declaring empty vector of size
vector<int> vec_2(6);
// using copy_if() function to copy in vector 2
copy_if(vec_1.begin(), vec_1.end(), vec_2.begin(), [](int i){return i%2==0;});
//print new vector
cout<<"Elements in vector v2 copied from v1: ";
for(int i=0; i<4; i++){
cout<<vec_2[i] << " ";
}
}

### Output

Output of this code will be −

Elements in vector v2 copied from v1: 10 30 40 0

## copy_backwards(start_i1, end_i1, end_i2)

This method is used to copy the data from one iterator to another iterator within specified range in the backward direction that means iterator will be moved to the end based upon its size and from that location elements will be pasted. It takes three types of arguments i.e.−

• Start_i1 − It will point to the initial element of the iterator, let’s say, i_1 from where the element will be copied to another iterator, let’s say, i_2.

• End_i1 − It will point to the end element of the iterator, let’s say, i_1 till where the element will be copied to another iterator, let’s say, i_2.

• end_i2 − It will point to the end location of an iterator at where the elements will be copied i.e. i_2.

Return Value − It will return an iterator pointing to the beginning element of the destination iterator i.e. i_2 till where the elements are copied.

### Example

Live Demo

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
//creating vector v1
vector<int> vec_1 = { 10, 21, 30, 40, 57,67 };
//declaring empty vector of size
vector<int> vec_2(6);
// using copy_backward() function to copy in vector 2
copy_backward(vec_1.begin(), vec_1.end()+4, vec_2.begin()+5);
//print new vector
cout<<"Elements in vector v2 copied from v1: ";
for(int i=0; i<vec_2.size(); i++){
cout<<vec_2[i] << " ";
}
}

### Output

Output of this code will be −

Elements in vector v2 copied from v1: 0 10 21 30 40 0
Updated on 14-Aug-2020 08:43:35