C++ find_if() Searches array of strings for first name and custom function

Description

C++ find_if() Searches array of strings for first name 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()/*  ww w  .jav a 2  s . co m*/
{
   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;
}



PreviousNext

Related