find、find_if

 1. find函数:若找到则返回指向该位置的迭代器,否则返回end;

template<class InputIterator,class T>
InputIterator find(InputIterator first,InputIterator last,const T&value)
{
  while(first!=last&&*first!=value)++first;
  return first;
}

  1)用于内建类型

vector<string> vec2;
string str1="apple";
string str2="banana";
string str3="grape";
string test_str="grape";
vec2.push_back(str1);
vec2.push_back(str2);
vec2.push_back(str3);
vector<string>::iterator ite3=find(vec2.begin(),vec2.end(),test_str);

      2)用于自定义类型,重载“==”运算符。

class people
{
public:
	char name;
	int age;

	people(char n,int a){name=n;age=a;}
	bool operator==(const people&p)
	{
		return (age==p.age);
	}
};
int main()
{
	people p1('z',20);
	people p2('q',30);
	people p3('l',40);
	people test('t',30);
	vector<people> vec;
	vec.push_back(p1);
	vec.push_back(p2);
	vec.push_back(p3);
	vector<people>::iterator ite=find(vec.begin(),vec.end(),test);
	cout<<(*ite).name;
	return 0;
}

 

2.find_if函数:是find的谓词版本,判断条件为仿函数(函数对象)。

template<class InputIterator,class Predicate>
InputIterator find(InputIterator first,InputIterator last,Predicate pred)
{
  while(first!=last&&!pred(*first))++first;
  return first;
}

  比较函数对象的时候需重载运算符“()”,两个例子如下,分别是map和vector:

用于map

#include <map> 
#include <string> 
  
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() 
   { 
       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 = my_map.end(); 
       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;         
   } 

 用于vector

 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 = my_vector.end(); 
       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); 
           
       getchar(); 
       return 0;         
   }

 

posted @ 2015-03-21 15:56  zhanghui_dut  阅读(87)  评论(0)    收藏  举报