C++ std::copy_n














































C++ std::copy_n



Description:

This C++ article is about the copy_n() algorithm function with std::vector container.
It is an STL algorithm in <algorithm> header file.It gives the freedom to choose how many
elements have to be copied in destination container

 
copy_n() has one definition:

template <class InputIterator, class Size, class OutputIterator>
OutputIterator copy_n (InputIterator first, Size n, OutputIterator result);


Return type :
An iterator to the end of the destination range where elements have been copied.

Working: The copy_n( ) function takes 3 parameters, two iterators 'first' ,n , result .

first : The pointer to the beginning of source container, from where elements have to be
started copying.
n:Integer specifying how many numbers would be copied to destination container starting
from n.If a negative number is entered, no operation is performed.
result:The pointer to the beginning of destination container, to where elements have to
be started copying.

Implementation: 

#include<iostream>
#include<algorithm> // copy_n()
#include<vector>
using namespace std;

int main()
{

   // initializing source vector
   vector<int> arr = { 1, 2, 4, 3, 9, 6 };

   vector<int> arr1(6);

   // using copy_n()
   copy_n(arr.begin(), 4, arr1.begin());

   // printing new vector
   cout << "The elements are : ";
   for(int i=0; i<arr1.size(); i++)
   cout << arr1[i] << " ";

}

Output:
The elements are : 1 2 4 3 0 0

More Articles of Mandeep Sheoran:

Name Views Likes
C++ program to insert an element into binary tree 7383 19
C++ program to find an element into binary tree 843 16
C++ std::is_void 626 15
C++ program to find the closest element in binary search tree 1072 19
C++ program to replace every element with the least greater element on its right 629 12
C++ program to delete an element into binary tree 855 24
C++ program to find maximum element between two nodes of binary search tree 751 20
C++ std::remove_copy_if with std::vectors 618 11
C++ program to print duplicate elements from the binary search tree 3175 15
C++ program to find depth of the deepest odd level node in binary tree 631 23
C++ program to remove duplicate elements from the binary search tree 1832 20
C++ std::rotate_copy with std::vector 605 14
C++ std::copy_n with std::vector 690 22
C++ std::copy_if with std::vector 1702 18
C++ program to print all the elements of binary search tree 8488 22
C++ std::reverse_copy with std::list 658 18
C++ program to print all the elements of binary tree 1424 18
C++ program to print all full nodes in a binary tree 619 25
C++ program to find sink odd nodes in binary tree 634 15
C++ std::is_copy_assignable 659 22
C++ program to check whether a binary tree is a full binary tree or not using recursion 696 19
C++ std::is_copy_constructible 679 27
C++ program to delete an element into binary search tree 3469 18
C++ std::reverse_copy with std::vector 537 18
C++ std::rotate with std::vector 863 15
C++ program to check for symmetric binary tree using recursion 654 25
C++ program to maximum sum from a tree with adjacent levels not allowed 618 15
C++ std::copy_n with std::list 666 21
C++ program to check if two trees are identical using recursion 599 15
C++ std::copy_n 990 21
C++ std::copy_if with std::list 1198 19
C++ program to print the nodes at odd levels of a tree 621 13
C++ program to find lowest common ancestor in a binary tree 739 29
C++ program to find depth of the deepest odd level leaf node 539 13
C++ std::remove_copy_if with std::list 741 20
C++ program to add all greater values to every node in a given binary search tree 715 15

Comments