STL中的find_if函数
上一篇文章也讲过,find()函数只能处理简单类型的内容,也就是缺省类型,如果你想用一个自定义类型的数据作为查找依据则会出错!这里将讲述另外一个函数find_if()的用法
这是find()的一个更强大的版本。这个例子演示了find_if(),它接收一个函数对象的参数作为参数, 并使用它来做更复杂的评价对象是否和给出的查找条件相付。
假设我们的list中有一些按年代排列的包含了事件和日期的记录。我们希望找出发生在1997年的事件。
代码如下:
- //----------------------------------------------------------------------------------------
- // Desc: STL_find_if()_How to find things in an STL list MkII
- // Author: pigfly
- // Data: 2010.12.01
- // Copyright (C) 2010 pigfly
- //----------------------------------------------------------------------------------------
- #include <iostream>
- #include <string>
- #include <list>
- #include <algorithm>
- using namespace std;
- class EventIsIn1997 {
- public:
- bool operator () (string& EventRecord) {
- // year field is at position 12 for 4 characters in EventRecord
- return EventRecord.substr(11,4)=="1997";
- //return this->substr(11,4)=="1997"
- }
- };
- int main (void) {
- list<string> Events;
- // string positions 0123456789012345678901234567890123456789012345
- Events.push_back("07 January 1995 Draft plan of house prepared");
- Events.push_back("07 February 1996 Detailed plan of house prepared");
- Events.push_back("10 January 1997 Client agrees to job");
- Events.push_back("15 January 1997 Builder starts work on bedroom");
- Events.push_back("30 April 1997 Builder finishes work");
- list<string>::iterator EventIterator = find_if (Events.begin(), Events.end(), EventIsIn1997());
- // find_if completes the first time EventIsIn1997()() returns true
- // for any object. It returns an iterator to that object which we
- // can dereference to get the object, or if EventIsIn1997()() never
- // returned true, find_if returns end()
- if (EventIterator==Events.end()) {
- cout << "Event not found in list" << endl;
- }
- else {
- cout << *EventIterator << endl;
- }
- }
输出:
10 January 1997 Client agrees to job
这里请注意,find_if()的第三个参数是EventIsIn1997(),它是个仿函数,接收一个string对象,在运算符()的内部定义我所要的查找条件,本例的查找条件是:EventRecord.substr(11,4)=="1997",注意,这里的仿函数返回类型必须是bool类型,这客观反应在find_if()函数查找过程中的是否匹配!
下面我们在看看,数据类型是自定义的结构体的查找过程:
代码:
- //----------------------------------------------------------------------------------------
- // Desc: STL_find_if() used in vector container, struct data
- // Author: pigfly
- // Data: 2010.12.01
- // Copyright (C) 2010 pigfly
- //----------------------------------------------------------------------------------------
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- struct value_t
- {
- int a;
- int b;
- };
- class vector_finder
- {
- public:
- vector_finder( const int a, const int b ) :m_v_a(a),m_v_b(b){}
- bool operator ()( vector<struct value_t>::value_type &value)
- {
- return (value.a==m_v_a)&&(value.b = m_v_b);
- }
- private:
- int m_v_a;
- int m_v_b;
- };
- int main()
- {
- vector<value_t> my_vector;
- value_t my_value;
- my_value.a = 11; my_value.b = 1001;
- my_vector.push_back(my_value);
- my_value.a = 12; my_value.b = 1002;
- my_vector.push_back(my_value);
- my_value.a = 13; my_value.b = 1003;
- my_vector.push_back(my_value);
- my_value.a = 14; my_value.b = 1004;
- my_vector.push_back(my_value);
- vector<value_t>::iterator it = find_if( my_vector.begin(), my_vector.end(), vector_finder(13,1003));
- if( it == my_vector.end() )
- cout<<"not found!"<<endl;
- else
- cout<<"found value a:"<<(*it).a <<", b:"<<(*it).b<<endl;
- return 0;
- }
输出:
found value a:13, b:1003
在这里,我们同样构造了一个仿函数,也就是class vector_finder,也就是vector_finder()函数,注意它的结构与我们要查找的结构体之间的关系,我们发现,它们是非常相象的。
这里的重点就在于class vector_finder的构造!
下面再看看,在map容器中的应用:
代码
- //----------------------------------------------------------------------------------------
- // Desc: STL_find_if() used in map container, string data
- // Author: pigfly
- // Data: 2010.12.01
- // Copyright (C) 2010 pigfly
- //----------------------------------------------------------------------------------------
- #include <iostream>
- #include <map>
- #include <string>
- #include <algorithm>
- using namespace std;
- class map_finder
- {
- public:
- map_finder( string cmp_string ) : m_string(cmp_string) {}
- bool operator () (const map<int,string>::value_type pair)
- {
- return pair.second == m_string;
- }
- private:
- string m_string;
- };
- int main()
- {
- map<int ,string> my_map;
- my_map.insert( make_pair(10,"china"));
- my_map.insert( make_pair(20,"usa"));
- my_map.insert( make_pair(30,"english"));
- my_map.insert( make_pair(40,"hongkong"));
- map<int,string>::iterator it = find_if(my_map.begin(),my_map.end(),map_finder("english"));
- if( it == my_map.end() )
- cout<<"not found!"<<endl;
- else
- cout<<"found key:"<<(*it).first<<", value:"<<(*it).second<<endl;
- return 0;
- }
输出:
found key:30,vlaue:english
由于这里只是讲究一下find_if()的用法,没有去关心它的细节,如果希望了解更多,比如仿函数的模板原形,甚至整个STL
STL中的find_if函数的更多相关文章
- C++STL中的unique函数解析
一.总述 unique函数属于STL中比较常用函数,它的功能是元素去重.即”删除”序列中所有相邻的重复元素(只保留一个).此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素给占领了(详细情 ...
- c++中STL中的next_permutation函数基本用法
对于next_permutation函数是针对于排列组合问题的库函数,它的排序方式是按照字典的方式排列的·: 如以下代码对于next_permutation函数的初步解释: #include<c ...
- 为什么map对象不能使用stl中的sort函数
STL所提供的各式各样算法中,sort()是最复杂最庞大的一个.这个算法接受两个RandomAccestlerators(随机存取迭代器),然后将区间内的所有元素以渐增方式由小到大重新排列.第二个版本 ...
- C++STL中的unique函数
头文件:#include<iostream> 函数原型:iterator unique(iterator it_1,iterator it_2); 作用:元素去重,即”删除”序列中所有相邻 ...
- stl中的for_each() 函数的注意事项
#include<iostream> using namespace std; #include"vector" #include"algorithm&quo ...
- 「STL中的常用函数 容器」
占个坑,下午在更 二分操作:lower_bound和upper_bound 存图/数列操作:vector容器 全排列:next_permutation和prev_permutation 字符串转数列: ...
- STL中一些函数的应用
1.nth_element():找到第几大的数.用法:nth_element(a,a+k,a+n),返回一个数组a中第k大的数,时间复杂度比较小,头文件#include <algorithm&g ...
- STL中的所有算法(70个)
STL中的所有算法(70个)----9种类型(略有修改by crazyhacking) 参考自: http://www.cppblog.com/mzty/archive/2007/03/14/1981 ...
- STL中的算法
STL中的所有算法(70个) 参考自:http://www.cppblog.com/mzty/archive/2007/03/14/19819.htmlhttp://hi.baidu.com/ding ...
随机推荐
- iOS子线程操作UI问题检查
iOS开发中,因为大部分函数都不是线程安全的,所以UI子线程中操作UI是非常危险的事,但是有时候因为开发者经验不足,不知道子线程中不能UI,或者知道但是写代码的时候没注意,或者不知道那些函数操作UI了 ...
- js apply
1.作用 函数的apply方法的作用与call方法类似,也是改变this指向,然后再调用该函数.唯一的区别就是,它接收一个数组作为函数执行时的参数 Fn.apply(obj, [arg1, arg2, ...
- 低字节序和高字节序相互转换(Little Endian/Big Endian)
这个例子展示了如何转换整形数字的字节顺序,该方法可以用来在little-endian和big-endian之间转换. 说明:Windos(x86,x64)和Linux(x86,x64)都是little ...
- 【COGS495】窗口
[问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表: Window position Min value Max val ...
- POJ 1155 树形背包(DP) TELE
题目链接: POJ 1155 TELE 分析: 用dp[i][j]表示在结点i下最j个用户公司的收益, 做为背包处理. dp[cnt][i+j] = max( dp[cnt][i+j ...
- mysql的sql优化案例
前言 mysql的sql优化器比较弱,选择执行计划貌似很随机. 案例 一.表结构说明mysql> show create table table_order\G***************** ...
- 实验四:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 如果我写的不好或者有误的地方请留言 ...
- Permission denied (publickey).
问题: ssh-keygen -t rsa -C "youremail@example.com" 若是接连按三次回车,会在-/.ssh下生成 id_rsa, id_rsa.pub ...
- oldboy第五天学习
一.生成器 列表生成器 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> b = [] >>> for i in ...
- codevs 1198 国王游戏
传送门 题目描述 Description 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n位 ...