C++ examples for STL Algorithm:find_if
Searches array of strings for first name using algorithm, find_if() and custom function
#include <iostream> #include <string> #include <algorithm> using namespace std; bool isDon(string name) // returns true if name=="Don" { return name == "Don"; } string names[] = { "G", "E", "Don", "M", "B" }; int main()/*from w w w . j a v a 2 s . com*/ { string* ptr; ptr = find_if( names, names+5, isDon ); if(ptr==names+5) cout << "Don is not on the list.\n"; else cout << "Don is element " << (ptr-names) << " on the list.\n"; return 0; }