.\" ident @(#)find_if.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH find_if 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2find_if\fP \ - Finds an occurrence of a value in a sequence that satisfies a specified predicate. .SH SYNOPSIS .br #include .br template .RE .RS 1 InputIterator find_if(InputIterator first, .RE .RS 23 InputIterator last, .br Predicate pred); .SH DESCRIPTION The find_if algorithm allows you to search for the first element in a sequence that satisfies a particular condition. The sequence is defined by iterators \f2first\fP and \f2last\fP, while the condition is defined by the third argument: a predicate function that returns a boolean value. find_if returns the first iterator \f2i\fP in the range \f2[first, last)\fP for which the following condition holds: \f2pred(*i) == true.\fP If no such iterator is found, find_if returns \f2last\fP. .SH COMPLEXITY find_if performs at most \f2last-first\fP applications of the corresponding predicate. .SH EXAMPLE .RE .RS 0 / .br // find.cpp .br // .br #include .br #include .br #include .br #include .br using namespace std; .RE .RS 1 .RE .RS 0 int main() .RE .RS 1 { .RE .RS 2 typedef vector::iterator iterator; .br int d1[10] = {0,1,2,2,3,4,2,2,6,7}; .RE .RS 0 .RE .RS 3 // Set up a vector .RE .RS 2 vector v1(d1,d1 + 10); .RE .RS 0 .RE .RS 3 // Try find .RE .RS 2 iterator it1 = find(v1.begin(),v1.end(),3); .RE .RS 3 // it1 = v1.begin() + 4; .RE .RS 0 .RE .RS 3 // Try find_if .RE .RS 2 iterator it2 = .RE .RS 4 find_if(v1.begin(),v1.end(),bind1st(equal_to(),3)); .RE .RS 3 // it2 = v1.begin() + 4 .RE .RS 0 .RE .RS 3 // Try both adjacent_find variants .RE .RS 2 iterator it3 = adjacent_find(v1.begin(),v1.end()); .RE .RS 3 // it3 = v1.begin() +2 .RE .RS 0 .RE .RS 2 iterator it4 = .RE .RS 5 adjacent_find(v1.begin(),v1.end(),equal_to()); .RE .RS 3 // v4 = v1.begin() + 2 .RE .RS 0 .RE .RS 3 // Output results .RE .RS 2 cout << *it1 << " " << *it2 << " " << *it3 << " " .RE .RS 8 << *it4 << endl; .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br 3 3 2 2 .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you have to write: \f2vector >\fP instead of: \f2vector\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO adjacent_find, Algorithms, find,_find_end, find_first_of