C++ Array search via algorithm find_if() and custom function

Description

C++ Array search via 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.ja va 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