stl.find_if用法总结

有时我们要在map、vector容器中查找符合条件的记录,map提供一个find的成员函数,但也仅限于查找关键字满足条件的记录,不支持值域的比较。如果我们要在值域中查找记录,该函数就无能无力了。而vector甚至连这样的成员函数都没有提供。所以一般情况下进行值域的查找,要么自己遍历数据,要么求助于STL的find_if函数。前种方法我们这里就不赘述了,只讲find_if函数。

1 -- find_if的STL定义
template <class InputIterator, class Predicate>
InputIterator find_if(InputIterator first, InputIterator last,Predicate pred)
{
       while (first != last && !pred(*first)) ++first;
       return first;
}

find_if是一个模板函数,接受两个数据类型:InputItearator迭代器,Predicate用于比较数值的函数或者函数对象(仿函数)。find_if对迭代器要求很低,只需要它支持自增操作即可。当前遍历到的记录符合条件与否,判断标准就是使得pred()为真。至此可能还有些不是很明了,下面举几个例子实际操练下的它的用法。

2 -- find_if在std::map查找时的应用

假如我们有个map对象是这么声明的:

std::map<int, std::string> my_map;
my_map.insert(std::make_pair(10, "china"));
my_map.insert(std::make_pair(20, "usa"));
my_map.insert(std::make_pair(30, "english"));
my_map.insert(std::make_pair(40, "hongkong"));

插入值后我们想得到值为”english”的这条记录,要怎样写程序呢?下面是个范例参考下:


#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>

class map_finder
{
public:
	map_finder(const std::string &cmp_string):m_s_cmp_string(cmp_string){}
	bool operator ()(const std::map<int, std::string>::value_type &pair)
	{
		return pair.second == m_s_cmp_string;
	}

private:
	const std::string &m_s_cmp_string;                    
};

int main(int argc, char ** argv)
{
	std::map<int, std::string> my_map;
	my_map.insert(std::make_pair(10, "china"));
	my_map.insert(std::make_pair(20, "usa"));
	my_map.insert(std::make_pair(30, "english"));
	my_map.insert(std::make_pair(40, "hongkong"));    

	std::map<int, std::string>::iterator it = std::find_if(my_map.begin(), my_map.end(), map_finder("english"));
	if (it == my_map.end())
		printf("not found\n");       
	else
		printf("found key:%d value:%s\n", it->first, it->second.c_str());

	return 0;        
}

class map_finder即用于比较的函数对象,它的核心就是重载的()运算符。因为每个容器迭代器的*运算符得到的结果都是该容器的value_type值,所以该运算符的形参就是map迭代器指向的value_type类型的引用。而map的value_type到底是什么类型,就得看下STL的源代码是如何定义的。

template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>
class map
{
public:
    typedef Key key_type;
    typedef pair<const Key, T> value_type;
    ......
};

从上面的定义可以看出,map的value_type是std::pair<const Key, t>类型,它的first值就是关键字,second值保存map的值域。

3 -- find_if在std::vector的应用

vector的find_if用法与map的很相似,区别仅仅是二者的value_type不一样而已。我们看下vecotr对value_type的定义。

template <class T, class Alloc = alloc>
class vector
{
public:
    typedef T value_type;
    typedef value_type* iterator;
    ......
};

可以看出vector的value_type就是容器的值类型,了解了这点,我们做个vector的find_if示范。


#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>

struct value_t
{
	int a;
	int b;
};

class vector_finder
{
public:
	vector_finder(const int a):m_i_a(a){}
	bool operator ()(const std::vector<struct value_t>::value_type &value)
	{
		return value.a == m_i_a;
	}

private:
	int m_i_a;                    
};


int main()
{
	std::vector<struct value_t> my_vector;
	struct value_t my_value;

	my_value.a = 11; my_value.b = 1000;
	my_vector.push_back(my_value);

	my_value.a = 12; my_value.b = 1000;
	my_vector.push_back(my_value);

	my_value.a = 13; my_value.b = 1000;
	my_vector.push_back(my_value);

	my_value.a = 14; my_value.b = 1000;
	my_vector.push_back(my_value);

	std::vector<struct value_t>::iterator it = std::find_if(my_vector.begin(), my_vector.end(), vector_finder(13));
	if (it == my_vector.end())
		printf("not found\n");       
	else
		printf("found value.a:%d value.b:%d\n", it->a, it->b);

	return 0;        
}
posted @ 2009-02-01 15:58  酱油和醋  阅读(34580)  评论(0编辑  收藏  举报