algorithm - STL C++

find_if()

Declaration

template <class InputIterator, class Predicate>
void find_if(
	InputIterator xFirst,
	InputIterator xLast,
	Predicate xTest
);

Description

This function finds the first entry that satisfies "xTest" in the range starting from "xFirst" up to the entry before "xLast."

Header Include

#include <algorithm>

Example

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

bool Test(char cChar) {
	return (cChar > 'm');
}

int main()
{
	using namespace std;

	// Create a vector instance
	vector<char> qV;
	qV.push_back('X');
	qV.push_back('o');
	qV.push_back('a');
	qV.push_back('X');
	qV.push_back('.');
	qV.push_back('n');
	qV.push_back('e');
	qV.push_back('t');

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

	qIter = find_if(qV.begin(), qV.end(), Test);

	// Output the vector after the find
	cout << "Found entry and beyond: ";
	for (; qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

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

Output

find_if() Output
 

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