STL--常用查找方法

常用查找方法

头文件
(1)find(v.begin(), v.end().4):查找值或者对象,查找对象时需要在类中有=运算符重载;
(2)find_if:查找关于仿函数的数据,也可以自定义数据和适配器搭配使用;
(3)adjacent_find(begin,end);查找第一个相同数据的迭代器;
(4)binary_search(begin,end,val):二分查找;返回值bool;
(5)count(begin,end,val):计算val的个数;返回值int;
(6)count_if(begin,end,greaterThen 5):计算大于5的个数;

代码使用

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;
//-------------------------------------------------------------------------------------------------
//find查找
class Person
{
public:
	Person(string name , int age)
	{
		this->m_Name =name ;
		this->m_Age =age;
	}
	bool operator==(const Person &p)
	{
		if(m_Name==p.m_Name  &&  m_Age == p.m_Age)
		{
			return true;
		}
		return false;
	}
	string m_Name;
	int m_Age;
};
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(),4); //(1)find查值
	cout<<*it<<endl;
	//自定义对象
	vector<Person> vv;
	vv.push_back(Person("aaa",10));
	vv.push_back(Person("bbb",20));
	vv.push_back(Person("ccc",30));
	vv.push_back(Person("ddd",40));

	vector<Person>:: iterator it1= find(vv.begin(),vv.end(),Person("ccc",30));//(2)find查对象,需要有==运算符重载
	cout<<it1->m_Name<<it1->m_Age<<endl;

}
//-------------------------------------------------------------------------------------------------
//find_if查找

class myCompare
{
public:
	bool operator()(int val)
	{
		return val>5;
	}
};
class mycompare:public binary_function<Person*, Person*,bool>
{
public:
	bool operator()(Person *p1,Person *p2) const
	{
		if(p1->m_Name==p2->m_Name  &&  p1->m_Age == p2->m_Age)
		{
			return true;
		}
		return false;
	}
};
void test02()
{
	vector<int> v;
	for(int i=0;i<10;i++)
	{
		v.push_back(i);
	}
	vector<int>:: iterator it = find_if(v.begin(),v.end(),myCompare());//(1)find_if 利用仿函数判断大于5的第一个true; 
	cout<<*it<<endl;

	//自定义对象
	vector<Person*> vv;
	Person p1("aaa",10);
	Person p2("bbb",20);
	Person p3("ccc",30);
	Person p4("ddd",40);

	vv.push_back(&p1);
	vv.push_back(&p2);
	vv.push_back(&p3);
	vv.push_back(&p4);
	
	Person* p= new Person("ccc",30);
	vector<Person*>:: iterator it1= find_if(vv.begin(),vv.end(),bind2nd(mycompare(),p));//(2)find_if利用适配器,比较自定义数据;
	if(it1!= vv.end())
	{
		cout<<(*it1)->m_Name<<(*it1)->m_Age<<endl;
	}
	else
	{
		cout<<" 未找到 "<<endl;
	}
}
//----------------------------------------------------------------------------------------------------------------------------
//adjacent_find查找
void test04()
{
	vector<int> v;
	for(int i=0;i<10;i++)
	{
		v.push_back(i);
	}
	v.push_back(5);
	v.push_back(5);
	v.push_back(5);

	vector<int> :: iterator it=adjacent_find(v.begin(),v.end());
	if(it!=v.end())
		cout<<*it<<endl;
}
//----------------------------------------------------------------------------------------------------------------------------
//binary_search二分查找
void test05()
{
	vector<int> v;
	for(int i=0;i<10;i++)
	{
		v.push_back(i);
	}

	bool ret = binary_search(v.begin(),v.end(),4);
	if(ret)
		cout<<"找到了"<<endl;
}
//----------------------------------------------------------------------------------------------------------------------------
//count,count_if 计数
class greaterThen5:public unary_function<int,bool>
{
public:
	bool operator()(int val) const
	{
		return val>5;
	}
};
void test06()
{
	vector<int> v;
	for(int i=0;i<10;i++)
	{
		v.push_back(i);
	}
	v.push_back(4);
	v.push_back(4);
	v.push_back(4);
	v.push_back(4);
	v.push_back(4);

	int num = count(v.begin(),v.end(),4);
	cout<<"4的个数:"<<num<<endl;

	int num1= count_if(v.begin(),v.end(),greaterThen5());
	cout<<"大于5的个数:"<<num1<<endl;

	int num2= count_if(v.begin(),v.end(),not1(greaterThen5()));
	cout<<"小于5的个数:"<<num2<<endl;
}

int main()
{
	test06();
	//test05();
	//test04();
	//test02();
	//test01();
	return 0;
}
发布了52 篇原创文章 · 获赞 14 · 访问量 5608

猜你喜欢

转载自blog.csdn.net/YanWenCheng_/article/details/104084493