函数原型:
template < class InputIterator , class Predicate >
InputIterator find_if( InputIterator first , InputIterator last , Predicate pred )
{
for( ; first != last ; first++ ) if( pred( *first ) ) break ;
return first ;
}
它在区间 [ first , last ] 中搜寻使一元判断式 pred 为 true 的第一个元素。
如果没找到,返回 end。
程序实例:
uva 644 - Immediate Decodability
main.cpp:
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
using namespace std;
bool Equal( string ch1 , string ch2 )
{
int len = min( ch1.size() , ch2.size() ) ;
for( int i = 0 ; i < len ; i++ ) {
if( ch1[ i ] != ch2[ i ] ) return false ;
}
return true ;
}
int main()
{
string str ;
int cas = 1 ;
bool ok = true ;
vector <string> group ; //定义 string 容器
while( !cin.eof() ) {
getline( cin, str ) ;
if( str == "9" ) { //标志一组数据的结束
printf( "Set %d is " , cas++ ) ;
if( !ok ) cout << "not immediately decodable\n" ;
else cout << "immediately decodable\n" ;
ok = true ;
group.clear() ; //清空容器
continue ;
}
if( !ok ) continue ; //如果 ok 已为 false 后面的就不用判断
/* 下面判断读入的 str 是否与 group 里面某个元素满足 not immediately
若满足,ok 记为 false
*/
if( find_if( group.begin() , group.end() , bind1st( ptr_fun( Equal ), str ) ) != group.end() )
ok = false ;
group.push_back( str ) ;
}
return 0;
}
关于 bind1st 请查阅相关资料,这里还未发现好的解释,若有,请告知,不胜感激!