1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <algorithm>
#include <string>
#include <cstring>
#include <iterator>
#include <iostream>
using namespace std;
class CaseName
{
std::string d_string;
public:
CaseName(char const *str): d_string(str)
{}
bool operator()(std::string const &element) const
{
return strcasecmp(element.c_str(), d_string.c_str()) == 0;
}
};
int main()
{
string sarr[] =
{
"Alpha", "Bravo", "Charley", "Delta", "Echo"
};
string *last = sarr + sizeof(sarr) / sizeof(string);
copy
(
find_if(sarr, last, CaseName{ "charley" }),
last, ostream_iterator<string>{ cout, " " }
);
cout << '\n';
if (find_if(sarr, last, CaseName{ "india" }) == last)
{
cout << "`india' was not found in the range\n";
copy(sarr, last, ostream_iterator<string>{ cout, " " });
cout << '\n';
}
}
/*
Displays:
Charley Delta Echo
`india' was not found in the range
Alpha Bravo Charley Delta Echo
*/
|