TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm copy_n() function

C++ algorithm copy_n() function tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm Functions copy_n()

C++ Algorithm copy_n() function specifies the number of elements to be copied into the new container. The function is used to copy n elements of the container [first,last) into a different container starting from result.

Syntax

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

Parameter

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.

result: It is an output iterator to the first element of the new container in which the elements are copied.

Return value

An iterator to the last element of the new range beginning with result is returned.

Example 1

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	vector<int> u1 = {2,6,8,4,9,4};
	vector<int> u2(6);
	vector<int> u3(6);
	copy(u1.begin(), u1.begin()+3, u2.begin());
	cout<<"The new vector with copy contains:";
	for(int k=0; k<u2.size(); k++)
	cout<<u2[k]<<" ";
	copy_n(u1.begin(),4,u3.begin());
	cout<<"\n";
	cout<<"The new vector using copy_n contains:";
	for(int m=0; m<u3.size(); m++)
	cout<<u3[m]<<" ";
}

Output:

The new vector with copy contains: 2 6 8 0 0 0 
The new vector using copy_n contains:2 6 8 4 0 0

Example 2

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	int newints[]={15,25,35,45,55,65,75};
	std::vector<int> newvector;
	newvector.resize(7);
	std::copy_n(newints,7,newvector.begin());
	std::cout<<"newvector contains:";
	for(std::vector<int>::iterator  ti= newvector.begin(); ti!=newvector.end();++ti)
	std::cout<<" "<<*ti;
	std::cout<<"\n";
	return 0;
}

Output:

newvector contains: 15 25 35 45 55 65 75

Complexity

The complexity of the function is linear starting from the first element to the last one.

Data races

Up to n elements of the container are accessed.

Exceptions

The function throws an exception if any of the container elements throws one.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf