algorithm - STL C++

find_first_of()

Declaration

template <class ForwardIterator1, class ForwardIterator2>
void find_first_of(
	ForwardIterator1 xFirst1,
	ForwardIterator1 xLast1,
	ForwardIterator2 xFirst2,
	ForwardIterator2 xLast2
);

Description

This function finds the first entry in the range starting from "xFirst1" up to the entry before "xLast1" that are equal to one of the entries in the range from "xFirst2" up to the entry before "xLast2." The overloaded version takes a predicate function and uses that to compare entries instead of equality.

Header Include

#include <algorithm>

Overloads

template <class ForwardIterator1, class ForwardIterator2, class Predicate>
void find_first_of(
	ForwardIterator1 xFirst1,
	ForwardIterator1 xLast1,
	ForwardIterator2 xFirst2,
	ForwardIterator2 xLast2,
	Predicate xTest
);

Example

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

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;

	// Search for any of these entries
	cout << "Searching for the first of any: ";
	for (qIter = qV.begin() + 3; qIter != qV.begin() + 5; ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	qIter = find_first_of(qV.begin(), qV.end(), qV.begin() + 3, qV.begin() + 5);

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

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