Searches array of strings for first name using algorithm, find_if() and custom function - C++ STL Algorithm

C++ examples for STL Algorithm:find_if

Description

Searches array of strings for first name using algorithm, find_if() and custom function

Demo Code

#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;
}

Result


Related Tutorials