algorithm - STL C++

copy_backward()

Declaration

template <class BidirIterator1, class BidirIterator2>
OutputIterator copy_backward(
	BidirIterator1 qFirst,
	BidirIterator1 qLast,
	BidirIterator2 qDest
);

Description

This function copies the entries from "qFirst" to the one before "qLast" to the location specified by "qDest," writing over the entries backward from "qDest."

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, backward from dest.
	copy_backward(qV.begin(), qV.end(), qCopy.begin() + 4);

	// 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_backward() Output
 

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