STL算法使用之std::find,std::find_i
2013-03-01 11:37
489 查看
STL的find,find_if函数提供了一种对数组、STL容器进行查找的方法。使用该函数,需 #include <algorithm>find示例一我们查找一个list中的数据,通常用find(),例如:
那么,如果容器里的元素是一个类呢?例如,有list<CPerson> ,其中CPerson类定义如下:
这个“相等”的定义,是通过重载“==”操作符实现的,我们在CPerson类中添加一个方法,定义为:
有人说,如果我有自己定义的“相等”呢?例如,有一个list<CPerson*>,这个list中的每一个元素都是一个对象的指针,我们要在这个list中查找具有指定age的元素,找到的话就得到对象的指针。
这时候,你不再能像上面的例子那样做,我们需要用到find_if函数,并自己指定predicate function(即find_if函数的第三个参数,请查阅STL手册)。
我们在CPerson类外部定义这样一个结构体:
using namespace std; int main() { list<int> lst; lst.push_back(10); lst.push_back(20); lst.push_back(30); list<int>::iterator it = find(lst.begin(), lst.end(), 10); // 查找list中是否有元素“10” if (it != lst.end()) // 找到了 { // do something } else // 没找到 { // do something } return 0; }find示例二
那么,如果容器里的元素是一个类呢?例如,有list<CPerson> ,其中CPerson类定义如下:
class CPerson { public: CPerson(void); ~CPerson(void); public: int age; // 年龄 };那么如何用find()函数进行查找呢?这时,我们需要提供一个判断两个CPerson对象“相等”的定义,find()函数才能从一个list中找到与指定的CPerson“相等”的元素。
这个“相等”的定义,是通过重载“==”操作符实现的,我们在CPerson类中添加一个方法,定义为:
bool perator==(const CPerson &rhs) const; 实现为: bool CPerson::operator==(const CPerson &rhs) const { return (id == rhs.age); } 然后我们就可以这样查找(假设list中已经有了若干CPerson对象)了: list<CPerson> lst; ////////////////////////////////// // 向lst中添加元素,此处省略 ////////////////////////////////// CPerson cp_to_find; // 要查找的对象 cp_to_find.age = 50; list<CPerson>::iterator it = find(list.begin(), list.end(), cp_to_find); // 查找 if (it != lst.end()) // 找到了 { // do something } else // 没找到 { // do something }find_if函数示例
有人说,如果我有自己定义的“相等”呢?例如,有一个list<CPerson*>,这个list中的每一个元素都是一个对象的指针,我们要在这个list中查找具有指定age的元素,找到的话就得到对象的指针。
这时候,你不再能像上面的例子那样做,我们需要用到find_if函数,并自己指定predicate function(即find_if函数的第三个参数,请查阅STL手册)。
我们在CPerson类外部定义这样一个结构体:
typedef struct finder_t { finder_t(int n) : age(n) { } bool operator()(CPerson *p) { return (age == p->age); } int age; }finder_t;然后就可以利用find_if函数来查找了:
list<CPerson*> lst;//////////////////////////////////// 向lst中添加元素,此处省略//////////////////////////////////list<CPerson*>::iterator it = find_if(lst.begin(), lst.end(), finder_t(50)); // 查找年龄为50的人if (it != lst.end()) // 找到了{cout << "Found person with age : " << (*it)->age;}else // 没找到{ // do something }
相关文章推荐
- (原創) 如何删除std::vector内的element?(使用find) (C/C++) (STL)
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- 使用STL通用算法find_if()在list中搜索对象
- 【C++ STL应用与实现】5: 如何使用std::array (since C++11)
- stl中的容器、迭代器和算法----vector中的find实现
- STL STD::list使用说明
- STL_算法_查找算法(find、find_if)
- STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)
- STL 查找vector容器中的指定对象:find()与find_if()算法
- 【C++ STL应用与实现】86: 如何使用std::accumulate
- C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- (原創) 如何将array转成std::vector? (使用constructor) (C/C++) (STL)
- c++ STL常用算法使用方法
- STL的find_if的使用
- C++ 之高效使用STL ( STL 算法分类)
- (原創) 如何使用find() algorithm? (C/C++) (STL)
- 【C++ STL应用与实现】56: 使用std::unique删除重复元素
- VC中STL std::string类的使用