copy_n() Function in C++ STL Last Updated : 27 Nov, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Copy_n() is the C++ function defined in <algorithm> library in STL. It helps to copy one array element to the new array. Copy_n function allows the freedom to choose how many elements must be copied in the destination container. This function takes 3 arguments, the source array name, the size of the array, and the target array name. Function Template: template <class InputIterator, class Size, class OutputIterator> OutputIterator copy_n (InputIterator start_limit, Size count, OutputIterator result); Parameters: start_limit - Input iterator pointing to the beginning of the range of elements to copy from.count - Number of elements to copy.result - Output iterator to the initial position in the new container. Time Complexity: O(n)Auxiliary Space: O(n) Example 1: C++ // C++ code to demonstrate the // working of copy_n() function // Used with array #include <algorithm> #include <iostream> using namespace std; int main() { // Initializing the array int ar[6] = { 8, 2, 1, 7, 3, 9 }; // Declaring second array int ar1[6]; // Using copy_n() to copy contents copy_n(ar, 6, ar1); // Displaying the new array cout << "The new array after copying is : "; for (int i = 0; i < 6; i++) { cout << ar1[i] << " "; } return 0; } OutputThe new array after copying is : 8 2 1 7 3 9 Below is the code demonstrating the use of the copy_n() function for vectors. Example 2: C++ // C++ code to demonstrate the // working of copy_n() function // Used with vector #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { // initializing the source vector vector<int> v1 = { 8, 2, 1, 7, 3, 9 }; // declaring destination vectors vector<int> v2(6); // using copy_n() to copy first 3 elements copy_n(v1.begin(), 3, v2.begin()); // printing new vector cout << "The new vector after copying is : "; for (int i = 0; i < v2.size(); i++) { cout << v2[i] << " "; } return 0; } OutputThe new vector after copying is : 8 2 1 0 0 0 Comment More infoAdvertise with us Next Article strol() function in C++ P pushpeshrajdx01 Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2022 STL cpp-algorithm-library +1 More Practice Tags : CPPSTL Similar Reads strol() function in C++ The strtol() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long int.This function also sets an end pointer that points to the first character after the last valid numeric character of the string, if there is no such characte 3 min read std::function in C++ The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s 5 min read copysign() function in C++ copysign(x, y) function returns the value with a magnitude of x and the sign of y. Examples: Input : copysign(6, -2) Output : -6 Input : copysign(-6, 2) Output : 6 Syntax: copysign(x, y); Parameters: x : Value with the magnitude y : Value with the sign Returns : Returns the value with a magnitude of 1 min read scalbln() function in C++ STL The scalbln() is a built-in function in C++ STL which takes two arguments and scales x by FLT_RADIX raised to the power n. The function returns the product of x and FLT_RADIX raised to the power n. FLT_RADIX: It is the value of the radix (integer base) of the exponent representation. Syntax: scalbln 2 min read wcsncat() function in C/C++ The wcsncat() function appends the characters of the source to the destination, including a terminating null wide character. If the length of the string in source is less than num. Then only the content up to the terminating null wide character is copied. Syntax: wchar_t* wcsncat (wchar_t* destinati 2 min read lldiv() function in C++ STL The lldiv() is a builtin function in C++ STL which gives us the quotient and remainder of the division of two numbers. Syntax: lldiv(n, d) Parameters: The function accepts two mandatory parameters which are described below: n: It specifies the dividend. The data-type can be long long or long long in 2 min read Like