algorithm - STL C++

remove_copy_if()

Declaration

template <class InputIterator, class OutputIterator, class Predicate>
OutputIterator remove_copy_if(
	InputIterator qFirst,
	InputIterator qLast,
	OutputIterator qDest,
	Predicate xTest
);

Description

This function copies the ordered entries, not including entries that satisfy the test function "xTest," from "qFirst" to the one before "qLast" to the location specified by "qDest," writing over the entries. The function returns a pointer to the end of the newly copied range.

Header Include

#include <algorithm>

Example

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

bool IsAnO(char cChar) {
	return (cChar == 'o');
}

int main()
{
	using namespace std;

	// Create two vector instances
	vector<char> qV1;
	qV1.push_back('X');
	qV1.push_back('o');
	qV1.push_back('a');
	qV1.push_back('X');
	qV1.push_back('.');
	qV1.push_back('n');
	qV1.push_back('e');
	qV1.push_back('t');

	vector<char> qV2(10, '-');

	vector<char>::iterator qIter;
	// Output the vectors
	cout << "Original V1: ";
	for (qIter = qV1.begin(); qIter != qV1.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	cout << "Original V2: ";
	for (qIter = qV2.begin(); qIter != qV2.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	// Copy v1 with the 'o' characters removed
	vector<char>::iterator qEndOfCopyIter;
	qEndOfCopyIter = remove_copy_if(qV1.begin(),  qV1.end(), qV2.begin() + 2, IsAnO);

	// Output the result vector
	cout << "Resulting V2: ";
	for (qIter = qV2.begin(); qIter != qV2.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

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

Output

remove_copy_if() Output
 

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