C++ Programming Code Examples
Learn C++ Language
Algorithm Library copy() Function in C++ Programming Language
Algorithm Library copy() Function in C++
copy() function is used to copy items from one iterator to another iterator with a specific range. We can define the start and end position of the source and it will copy all items in this rage to a different destination. To use copy() function, we need to include <bits/stdc+.h> or header file.
It copies all the elements pointed by first and last. first element is included in the output but last is not. output is the start position of the final result iterator. It returns one iterator to the end of the destination range where elements have been copied.
Syntax for copy() Function in C++
template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);
first
It is an input iterator to the first element of the range, where the element itself is included in the range.
last
It is an input iterator to the last element of the range, where the element itself is not included in the range.
Input iterators to the initial and final positions in a sequence to be copied. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
result
It is an output iterator to the first element of the new container in which the elements are copied. Output iterator to the initial position in the destination sequence. This shall not point to any element in the range [first,last).
Function returns an iterator to the end of the destination range where elements have been copied.
Complexity
Linear in the distance between first and last: Performs an assignment operation for each element in the range.
Data races
The objects in the range [first,last) are accessed (each object is accessed exactly once). The objects in the range between result and the returned value are modified (each object is modified exactly once).
Exceptions
Throws if either an element assignment or an operation on iterators throws. Note that invalid arguments cause undefined behavior.
/* copying the array elements to the vector by copy() function code example */
// C++ STL program to demonstrate use of std::copy() function
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
//declaring & initializing an int array
int arr[] = { 10, 20, 30, 40, 50 };
//vector declaration
vector<int> v1(5);
//copying array elements to the vector
copy(arr, arr + 5, v1.begin());
//printing array
cout << "arr: ";
for (int x : arr)
cout << x << " ";
cout << endl;
//printing vector
cout << "v1: ";
for (int x : v1)
cout << x << " ";
cout << endl;
return 0;
}
Write a 'C++' program which Counts numbers of vowels in a given string and tell every index where a 'Vowel is Found'. Size of array is fixed using the constant variable. We enter a string
First 'Compare the Element' at the beginning with another array element sequentially. And swap values if the element at the beginning is Larger than the other element. This value will