使用STL通用算法find_if()在list中搜索对象
2007-07-25 15:34
609 查看
这是find()的一个更强大的版本。这个例子演示了find_if(),它接收一个函数对象的参数作为参数, 并
使用它来做更复杂的评价对象是否和给出的查找条件相付。
假设我们的list中有一些按年代排列的包含了事件和日期的记录。我们希望找出发生在1997年的事件。
/*
|| How to find things in an STL list MkII
*/
#include <string>
#include <list>
#include <algorithm>
class EventIsIn1997 {
public:
bool operator () (string& EventRecord) {
// year field is at position 12 for 4 characters in EventRecord
return EventRecord.substr(12,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
使用它来做更复杂的评价对象是否和给出的查找条件相付。
假设我们的list中有一些按年代排列的包含了事件和日期的记录。我们希望找出发生在1997年的事件。
/*
|| How to find things in an STL list MkII
*/
#include <string>
#include <list>
#include <algorithm>
class EventIsIn1997 {
public:
bool operator () (string& EventRecord) {
// year field is at position 12 for 4 characters in EventRecord
return EventRecord.substr(12,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
相关文章推荐
- 关于在STL容器list中使用find_if搜索特定的节点
- STL 查找vector容器中的指定对象:find()与find_if()算法
- STL 查找vector容器中的指定对象:find()与find_if()算法
- stl 中list 或者vector正确使用find查找类对象
- STL之vector,数组线性容器array,list容器,算法find,find_if,bind1st,仿函数
- 关于在STL容器list中使用find_if 函数
- C++之STL(六):变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- STL中remove_if()/find_if()/replace_if()的使用方法(The usages of remove_if()/find_if()/replace_if() )
- STL_算法_查找算法(find、find_if)
- 使用find_if算法搜寻map的value
- STL中vector查找算法find()和find_if()深入比较
- STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL中泛型算法find_if()与sort()的应用举例及详解
- STL 统计vector容器中指定对象元素出现的次数:count()与count_if()算法
- STL常用算法: distance实现,count_if算法实现,bind2nd的使用..
- 从零开始学C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- 从零开始学C++之STL(六):变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- STL(六):变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)