find_first_of

C++ Library  
 

Header

<algorithm>
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2)

The algorithm locates, within the range [first1, last1), the first element of a sequence represented by the range [first2, last2). The non-predicate version uses the operator== for comparison.

template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pr)

The algorithm locates, within the range [first1, last1), the first element of a sequence represented by the range [first2, last2). The predicate version uses the predicate function pr for comparison.


Sample

#pragma warning (disable:4786)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <functional>


int main()
{
	char *input = "AABBCDEFAAEFFBB" ;
	char *pattern = "BB" ;

	int inputLen = strlen(input) ;
	int patternLen = strlen(pattern) ;
	
	char *pos ;

	//find first occurence of pattern in input
	//non-predicate version of find_first_of
	pos = std::find_first_of(input, input+inputLen, pattern, pattern+patternLen) ;

	std::cout << "non-predicate version of find_first_of" << std::endl ;
	std::cout << "first occurence of " << pattern << " in " << input 
		<< " is at position " << (pos - input) << std::endl ;

	//find first occurence of pattern in input
	//predicate version of find_first_of
	pos = std::find_first_of(input, input+inputLen, pattern, pattern+patternLen, std::equal_to<char>()) ;
	std::cout << "\n\npredicate version of find_first_of" << std::endl ;
	std::cout << "first occurence of " << pattern << " in " << input 
		<< " is at position " << (pos - input) << std::endl ;

	//find last occurence of pattern in input
	//non-predicate version of find_end
	pos = std::find_end(input, input+inputLen, pattern, pattern+patternLen) ;

	std::cout << "\n\nnon-predicate version of find_end" << std::endl ;
	std::cout << "last occurence of " << pattern << " in " << input 
		<< " is at position " << (pos - input) << std::endl ;

	//find last occurence of pattern in input
	//predicate version of find_end
	pos = std::find_end(input, input+inputLen, pattern, pattern+patternLen, std::equal_to<char>()) ;
	std::cout << "\n\npredicate version of find_end" << std::endl ;
	std::cout << "first occurence of " << pattern << " in " << input 
		<< " is at position " << (pos - input) << std::endl ;

	return 0 ;
}

Program Output

non-predicate version of find_first_of
first occurence of BB in AABBCDEFAAEFFBB is at position 2


predicate version of find_first_of
first occurence of BB in AABBCDEFAAEFFBB is at position 2


non-predicate version of find_end
last occurence of BB in AABBCDEFAAEFFBB is at position 13


predicate version of find_end
first occurence of BB in AABBCDEFAAEFFBB is at position 13

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.