常用的查找算法
算法简介:
- find//查找元素
- find_if//按条件查找元素
- adjacent_find//查找相邻重复元素
- binary_search//二分查找法
- count//统计元素个数
- count_if//按条件统计元素个数
find
功能描述:
查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
函数原型:
find(iterator beg, iterator end, value);
//按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器
//beg开始迭代器
//end结束迭代器
//value查找的元素
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
}
bool operator==(const Person&p) {//自定义数据类型必须要重载==,让底层知道怎么比较
if (this->m_name == p.m_name && this->m_age == p.m_age) {
return true;
}else{
return false;
}
}
string m_name;
int m_age;
};
void test1()
{
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 << "未找到元素" << endl;
}
else {
cout << "找到元素:" << *it << endl;
}
}
void test2()
{
vector<Person> v2;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v2.push_back(p1);
v2.push_back(p2);
v2.push_back(p3);
v2.push_back(p4);
vector<Person>::iterator it = find(v2.begin(), v2.end(),p2);
if (it == v2.end()) {
cout << "未找到元素" << endl;
}
else {
cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
}
}
int main(void)
{
// test1();
test2();
return 0;
}
总结:利用find可以在容器中找到指定元素,返回值是迭代器。
find_if
功能描述:
按条件查找元素
函数原型:
find_if(iterator beg, iterator end, _pred);
//按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器
//beg开始迭代器
//end结束迭代器
//_pred函数或者谓词(返回bool类型的仿函数)
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class Graterfive {
public:
bool operator()(int val) {//仿函数
return val > 5;
}
};
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
}
bool operator==(const Person& p) {//自定义数据类型必须要重载==,让底层知道怎么比较
if (this->m_name == p.m_name && this->m_age == p.m_age) {
return true;
}
else {
return false;
}
}
string m_name;
int m_age;
};
class Grater20 {
public:
bool operator()(const Person &p) {//自定义数据类型需要使用仿函数
return p.m_age > 20;
}
};
void test1()
{
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
vector<int>::iterator it =find_if(v.begin(),v.end(),Graterfive());//最后一个参数传入函数对象
if (it == v.end()) {//需要重载==号
cout << "未找到元素" << endl;
}
else {
cout << "找到大于5的元素:" << *it << endl;
}
}
void test2()
{
vector<Person> v2;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v2.push_back(p1);
v2.push_back(p2);
v2.push_back(p3);
v2.push_back(p4);
vector<Person>::iterator it = find_if(v2.begin(), v2.end(),Grater20());
if (it == v2.end()) {
cout << "未找此人" << endl;
}
else {
cout << "找到姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
}
}
int main(void)
{
// test1();
test2();
return 0;
}
adjacent_find
功能描述:
查找相邻重复元素
函数原型:
adjacent_find(iterator beg, iterator end);
//查找相邻重复元素,返回相邻元素的第一个位置的迭代器
//beg开始迭代器
//end结束迭代器
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void test1()
{
vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(3);
v.push_back(0);
v.push_back(2);
v.push_back(2);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it == v.end()) {
cout << "未找到相邻重复元素" << endl;
}
else {
cout << "找到相邻重复元素:" << *it << endl;
}
}
int main(void)
{
test1();
return 0;
}
binary_search
功能描述:
查找指定元素是否存在
函数原型:
bool binary_search(iterator beg, iterator end, value);
//查找指定的元素,查到返回true否则返回false
//beg开始迭代器
//end结束迭代器
注意:在无序序列中不可用。查到返回true否则返回false
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void test1()
{
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
//必须是有序数列
bool ret = binary_search(v.begin(), v.end(), 9);//查找9
if (ret) {
cout << "找到元素" << endl;
}
else {
cout << "未找到元素" << endl;
}
}
int main(void)
{
test1();
return 0;
}
count
功能描述:
统计元素个数
函数原型:
count(iterator beg, iterator end, value);
//统计元素出现次数
//beg开始迭代器
//end结束迭代器
//value统计的元素
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
void test1()//内置数据类型
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(40);
v.push_back(30);
v.push_back(40);
v.push_back(40);
int num1 = count(v.begin(), v.end(), 40);//统计与40相同的元素个数
cout << "num1 = " << num1 << endl;
}
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
}
bool operator==(const Person& p) {//一定要重载==号
if (this->m_age == p.m_age) {
return true;
}
else {
return false;
}
}
int m_age;
string m_name;
};
void test2()
{
Person p1("刘备", 35);
Person p2("张飞", 35);
Person p3("关羽", 35);
Person p4("赵云", 38);
Person p5("曹操", 40);
Person p("诸葛亮", 35);
vector<Person> v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num2 = count(v.begin(), v.end(), p);//统计与诸葛亮年龄相同的人的个数
cout << "num2 = " << num2 << endl;
}
int main(void)
{
// test1();
test2();
return 0;
}
总结:对于统计自定义数据类型时候,需要配合重载operator==
count_if
功能描述:
按条件统计元素个数
函数原型:
count_if(iterator beg, iterator end, _Pred);
//按条件统计元素出现次数
//beg开始迭代器
//end结束迭代器
//_Pred谓词
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class Grater20 {
public:
bool operator()(int val) {
return val > 20;
}
};
void test1()//内置数据类型
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(40);
v.push_back(30);
v.push_back(40);
v.push_back(40);
int num1 = count_if(v.begin(), v.end(), Grater20());//统计大于20的元素个数
cout << "num1 = " << num1 << endl;
}
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
}
int m_age;
string m_name;
};
class Graterage20 {
public:
bool operator()(const Person &p){
return p.m_age > 20;
}
};
void test2()
{
Person p1("刘备", 35);
Person p2("张飞", 35);
Person p3("关羽", 20);
Person p4("赵云", 21);
Person p5("曹操", 18);
vector<Person> v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num2 = count_if(v.begin(), v.end(),Graterage20());//统计年龄大于20的人数
cout << "num2 = " << num2 << endl;
}
int main(void)
{
test1();
test2();
return 0;
}