C++常用查找算法

find //查找元素
find_if //按条件查找元素
adjacent_find //查找相邻重复元素
binary_search //二分查找算法
count //统计元素个数
count_if //按条件统计元素个数

(1)find
查找指定元素,返回找到的指定元素的迭代器,找不到则返回结束迭代器
函数原型:
find(iterator beg,iterator end,value);
示例:

#include<iostream> 
 using namespace std;
 #include<vector>
 #include<string>
 #include<algorithm> 
 
 //查找自定义数据类型
 void test01(){
 	vector<int>v;
	 for(int i = 0;i<10;i++){
         v.push_back(i); 	
	 } 
	 
	 vector<int>::iterator it = find(v.begin(),v.end(),200);
	 if(it==v.end()){
	 	cout<<"没有找到!"<<endl; 
	 }  
	 else{
	 	cout<<"找到该元素:"<<*it<<endl;
	 }
 } 
 
 class Person{
   public:
	   string m_Name;
 	   int m_Age;
 	
 	    Person(string name,int age){
         this->m_Age = age;
		 this->m_Name = name;
		} 
	   //重载==号,底层find知道如何对比person数据类型
	    bool operator==(const Person &p){
	   	    if(this->m_Age==p.m_Age&&this->m_Name==p.m_Name){
	   	    	return true;
			}
			else{
				return false;
	 		}
	    } 		
	
 };
 
 void test02(){
 	vector<Person>v;
 	Person p1("张三",30);
 	Person p2("王二",23);
 	Person p3("麻子",25);
 	Person p4("李四",39);
 	
 	v.push_back(p1);
	v.push_back(p2);
    v.push_back(p3);
	v.push_back(p4);
	
	Person pp("里斯",111);
	
	//vector<Person>::iterator it = find(v.begin(),v.end(),p2);
	vector<Person>::iterator it = find(v.begin(),v.end(),pp);

     if( it == v.end()){
     	cout<<"没有找到该元素!"<<endl; 
	 } 
	 else{
	 	cout<< "找到该元素:姓名为: "<<it->m_Name<<"  年龄为: " <<it->m_Age<<endl; 
	 }	
 }

(2)find_if
函数原型:
find_if(iterator beg,iterator end,_Pred);
按值查找元素,找到返回指定位置的迭代器,找不到返回结束迭代器
示例

 class GreaterFive{
 	public:
 		bool operator()(int val){
 			return val>5;
		 }
 };
 
 //1、按内置数据类型按条件查找
 void test01(){
 	vector<int>v;
 	for(int i=0;i<10;i++){
	    v.push_back(i);	
	} 
	vector<int>::iterator  it = find_if(v.begin(),v.end(),GreaterFive());
	
	if(it==v.end()){
		cout<<"没有找到!"<<endl; 
	}
	else{
		cout<<"找到了大于5的数字为:"<<*it<<endl;
	}
 } 
   
 //2、按条件查找自定义数据类型
class Person{
	public:
		string m_Name;
		int m_Age;
		
		Person(string name,int age){
			this->m_Age = age;
			this->m_Name = name;
		} 
};

class Greater20{
	public:
		
	bool operator()(Person&p){
		return p.m_Age >20; 
	}
}; 

void test02(){
	
	vector<Person>v;
	//创建数据
	Person p1("aaa",10);
	Person p2("bbb",20);
	Person p3("ccc",30);
	Person p4("ddd",40);
	
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	
	//找一个年龄大于20的人
	vector<Person>::iterator dit = find_if(v.begin(),v.end(),Greater20()) ;
	
	 if(v.end() == dit){
		  cout<<"没有查到该人"<<endl; 
	}
	 else{
		cout<<"查到该人的姓名为 :"<<dit->m_Name<<"年龄为: "<<dit->m_Age<<endl; 
    }
}

(3)查找相邻重复的元素adjacent_find
函数原型:
adjacent_find(iterator beg,iterator end); //返回相邻重复元素的迭代器
示例:

void test01(){
	vector<int>v;
	v.push_back(0);
	v.push_back(2);
	v.push_back(0);
	v.push_back(3);
	v.push_back(1);
	v.push_back(4);
	v.push_back(3);
	//v.push_back(3);
	
	vector<int>::iterator it = adjacent_find(v.begin(),v.end()); 
	if(it==v.end()){
		cout<<"没有查到相邻重复元素"<<endl;
	}
	else{
		cout<<"相邻重复的元素为:"<<*it<<endl; 
	}
}

(4)binary_search
查找指定元素是否存在,
函数原型:
bool binary_search(iterator beg,iterator end,value); //查找指定元素,查到返回true,否则false
示例:

//binary_search算法 
void test01(){
	vector<int>v;
	for(int i = 0;i<10;i++){
	    v.push_back(i);
	}
	//注意:容器中必须是有序序列,若是无需序列,结果未知。 
	
	//v.push_back(2);
	//查找容器中是否有3元素
	bool ret = binary_search(v.begin(),v.end(),11);
	
	if(ret){
		cout<<"查到该元素"<<endl; 
	}
	else{
		cout<<"没有查到该元素"<<endl; 
	}
}

(5)count
统计元素个数
函数原型:
cout(iterator beg,iterator end,value);//统计元素出现次数
示例:

class Person{
  public:
	string m_Name;
	int m_Age;
	Person(string name,int age){
		this->m_Age = age;
		this->m_Name = name;
	}
	
	bool operator==(const Person &p){
		if(this->m_Age == p.m_Age){
			return true;
		}
		else{
			return false;
		}
	}
};

//count算法 
void test01(){
	vector<int>v;
    v.push_back(1);
	v.push_back(1);
	v.push_back(4);
	v.push_back(2);
	v.push_back(1);
	v.push_back(3);
	
	cout<<count(v.begin(),v.end(),1)<<endl;
	
	vector<Person>p;
	Person p1("张三",10);
	Person p2("里斯",40);
	Person p3("王五",10);
	Person p4("李四",10);
	Person p5("台南",20);
	
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);
	p.push_back(p5);
	
	Person p6("诸葛亮",10);
	
	int num = count(p.begin(),p.end(),p6);
	cout<<"与诸葛亮同岁的人员个数:"<<num<<endl;
}

(6)count_if
按条件统计元素个数
函数原型:
count_if(iterator beg,iterator end,_Pred) ; //按条件统计元素出现次数
示例:

//人员构造 
class Person{
   public:
	string m_Name;
	int m_Age;
	Person(string name,int age){
		this->m_Age = age;
		this->m_Name = name;
	}
};

//大于20的整数谓语 
class Greater20{
	public:
		bool operator()(int value){
			return value>20;
		}
};

//大于20岁的人员的谓语 
class GreaterAge20{
	public : 
	    bool operator()(Person &p){
	    	return p.m_Age>20;
		}
};

//count_if算法 
void test01(){
	vector<int>v;
    v.push_back(10);
	v.push_back(20);
	v.push_back(40);
	v.push_back(20);
	v.push_back(30);
	v.push_back(50);
	
	//输出大于20的元素个数 
	cout<<count_if(v.begin(),v.end(),Greater20())<<endl;
	
	vector<Person>p;
	Person p1("张三",10);
	Person p2("里斯",40);
	Person p3("王五",110);
	Person p4("李四",130);
	Person p5("台南",20);
	
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);
	p.push_back(p5);
	
	//按条件统计 
	int num = count_if(p.begin(),p.end(),GreaterAge20());
	cout<<" 大于20岁的人员个数: "<<num<<endl;
}
发布了31 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/souhanben5159/article/details/104060872