algorithm - STL C++

copy()

Declaration

template <class InputIterator, class OutputIterator>
OutputIterator copy(
	InputIterator qFirst,
	InputIterator qLast,
	OutputIterator qDest
);

Description

This function copies the entries from "qFirst" to the one before "qLast" to the location specified by "qDest," writing over the entries. The function returns an iterator that points to the end of the newly copied entries.

Header Include

#include <algorithm>

Example

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
	using namespace std;

	// Create a vector instance
	vector<int> qV;
	qV.push_back(3);
	qV.push_back(5);

	// Create another vector that we will copy into
	vector<int> qCopy;
	qCopy.push_back(100);
	qCopy.push_back(200);
	qCopy.push_back(300);
	qCopy.push_back(400);
	qCopy.push_back(500);

	vector<int>::iterator qIter;
	// Output the vector before the copy
	cout << "Before the copy:" << endl;
	for (qIter = qCopy.begin(); qIter != qCopy.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

	// Copy the first vector into the second
	copy(qV.begin(), qV.end(), qCopy.begin() + 1);

	// Output the vector after the copy
	cout << "After the copy:" << endl;
	for (qIter = qCopy.begin(); qIter != qCopy.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

	// Keep the window open
	cin.get();
	return 0;
}

Output

copy() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.