[C++STL]常用查找算法

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[C++STL]常用查找算法相关的知识,希望对你有一定的参考价值。

在这里插入图片描述
在这里插入图片描述
代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

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(), 5);
	if (it == v.end())
	{
		cout << "no find" << endl;
	}
	else
	{
		cout << "find " << *it << endl;
	}


}

class Person
{
public:
	Person(string name,int age):name(name),age(age){}

	bool operator==(const Person &p)
	{
		if (name == p.name && age == p.age)
		{
			return true;
		}
		return false;
	}


	string name;
	int age;
};

void test02()
{
	vector<Person>v;
	Person p1("Tom", 28);
	Person p2("Jack", 78);
	Person p3("Bom",18);
	Person p4("Lily", 21);
	Person p5("Mike", 38);
	
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	vector<Person>::iterator it = find(v.begin(), v.end(), p2);
	if (it == v.end())
	{
		cout << "no find" << endl;
	}
	else
	{
		cout << "name = " << it->name << " " << "age = " << it->age << endl;
	}

}

int main()
{
	test01();
	test02();
	return 0;
}

测试结果:

在这里插入图片描述

代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

class GreaterFive
{
public:
	bool operator()(int a)
	{
		return a > 5;
	}
};

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i+1);
	}

	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end())
	{
		cout << "no find" << endl;
	}
	else
	{
		cout << "find elem" << endl;
	}


}

class Person
{
public:
	
	Person(string name, int age) :name(name), age(age)
	{

	}

	bool operator==(const Person & p) {
		if (this->name == p.name && this->age == p.age)
		{
			return true;
		}
		return false;
	}

	string name;
	int age;
};


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

void test02()
{
	vector<Person> v;
	Person p1("Tom", 10);
	Person p2("Jack", 20);
	Person p3("Bom", 30);
	Person p4("Mike", 40);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());

	if (it == v.end())
	{
		cout << "no find" << endl;
	}
	else
	{
		cout << "name = " << it->name << "age = " << it->age << endl;
	}

}



int main()
{
	test01();
	test02();
	return 0;
}

测试结果:

在这里插入图片描述

在这里插入图片描述

代码如下:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;


void test01()
{
	vector<int>v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(7);
	v.push_back(8);
	v.push_back(5);
	v.push_back(4);
	v.push_back(4);

	vector<int>::iterator it = adjacent_find(v.begin(), v.end());
	if (it == v.end())
	{
		cout << "no find" << endl;
	}
	else
	{
		cout << "find" << " " << *it << endl;
	}

}

int main()
{
	test01();
	return 0;
}

测试结果:
在这里插入图片描述

总结:

在这里插入图片描述在这里插入图片描述

代码如下:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;


void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	bool ret = binary_search(v.begin(), v.end(),2);
	if (ret)
	{
		cout << "find" << endl;
	}
	else
	{
		cout << "no find" << endl;
	}
}

int main()
{
	test01();
	return 0;
}

测试结果:

在这里插入图片描述

总结:
在这里插入图片描述
在这里插入图片描述

代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;


void test01()
{
	vector<int>v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(7);
	v.push_back(8);
	v.push_back(5);
	v.push_back(4);
	v.push_back(4);
	
	int num = count(v.begin(), v.end(), 4);

	cout << "4 count = " << num << endl;
	

}

class Person
{
public:
	Person(string name,int age):name(name),age(age){}

	bool operator==(const Person &p)
	{
		if (age == p.age) return true;
		else
		{
			return false;
		}
	}


	string name;
	int age;
};

void test02()
{
	vector<Person> v;
	Person p1("Tom", 35);
	Person p2("Mike", 35);
	Person p3("Lily", 35);
	Person p4("Bom", 25);
	Person p5("Jike", 25);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	Person p("yhf", 35);
	int num = count(v.begin(), v.end(), p);
	cout << "num = " << num << endl;
}

int main()
{
	test01();
	test02();
	return 0;
}

测试结果:
在这里插入图片描述

总结:
在这里插入图片描述

在这里插入图片描述

代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class Greater20
{
public:
	bool operator()(int val)
	{
		return val > 20;
	}
};

void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);

	int num = count_if(v.begin(), v.end(), Greater20());
	cout << "num = " << num << endl;

}

class Person
{
public:
	Person(string name,int age):name(name),age(age){}
	string name;
	int age;
};

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

void test02()
{
	vector<Person>v;
	Person p1("Tom", 30);
	Person p2("Lily", 18);
	Person p3("Jack", 27);
	Person p4("Mike", 32);
	Person p5("Bom", 15);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	int num = count_if(v.begin(), v.end(), ageGreater20());

	cout << "age num = " << num << endl;
}



int main()
{
	test01();
	test02();
	return 0;
}

测试结果:

在这里插入图片描述

以上是关于[C++STL]常用查找算法的主要内容,如果未能解决你的问题,请参考以下文章

STL 常用方法

[C++STL]常用遍历算法

[C++STL]常用排序算法

[C++STL]常用算术生成算法

[C++STL]常用拷贝和替换算法

STL_算法_查找算法(findfind_if)