C++ find_if

函数原型:

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 请查阅相关资料,这里还未发现好的解释,若有,请告知,不胜感激!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值