COPY()
This function is used to copy elements from one range to another. It takes three iterators as arguments: the beginning and end of the source range, and the beginning of the destination range. The function then sequentially copies each element from the source to the destination in order, preserving their original order. The destination range must be large enough to hold all copied elements, as std::copy()
does not allocate new memory or check boundaries. It returns an iterator pointing to the element in the destination range just past the last copied element. Commonly used with arrays, vectors, or other STL containers, std::copy()
provides an efficient, type-safe way to duplicate data and is often preferred over manual loops for readability and maintainability.
copy(begin, end, destination);
* begin: The starting point of the source range (inclusive).
* end: The ending point of the source range (exclusive).
* destination: The beginning of the destination where the copied elements will
be stored.
#include <iostream>
#include <algorithm> // For copy
using namespace std;
int main() {
// Source array
int source[] = {1, 2, 3, 4, 5};
// Destination array with the same size
int destination[5];
// Copy elements from source to destination using copy
// (source + 5) moves that pointer forward by 5 elements; that is, source + 5
// points to the memory address right after source[4].
// Start copying from source[0]; Stop before source[5] (which doesn’t exist — it’s just the sentinel marking the end)
copy(source, source + 5, destination);
// Output the copied elements in destination
cout << "Destination array: ";
for (int i = 0; i < 5; i++) {
cout << destination[i] << " ";
}
cout << endl;
return 0;
}
OUTPUT:
Destination array: 1 2 3 4 5
#include <iostream>
#include <string>
#include <algorithm> // For copy
using namespace std;
int main() {
int n; // Number of employees
// Step 1: Take input for the number of employees
cin >> n;
string employeeNames[n]; // Declare an array to store employee names
string copiedNames[n]; // Declare another array to copy employee names
// Step 2: Input employee names
for (int i = 0; i < n; i++) {
cin >> employeeNames[i]; // Take input for each employee name
}
// Step 3: Use copy() to copy the names to the second array
copy(employeeNames, employeeNames + n, copiedNames);
// Step 4: Print the employee names from both arrays
cout << "Employee Names (Original): ";
for (int i = 0; i < n; i++) {
cout << employeeNames[i] << " "; // Print the original employee names
}
cout << endl;
cout << "Employee Names (Copied): ";
for (int i = 0; i < n; i++) {
cout << copiedNames[i] << " "; // Print the copied employee names
}
cout << endl;
// Step 5: Print the length of each name from copiedNames
cout << "Length of Employee Names (Copied): ";
for (int i = 0; i < n; i++) {
cout << copiedNames[i].length() << " "; // Print the length of each name
}
cout << endl;
return 0;
}
INPUT:
3
John
Alice
Bob
OUTPUT:
Employee Names (Original): John Alice Bob
Employee Names (Copied): John Alice Bob
Length of Employee Names (Copied): 4 5 3
Last updated