网站建设视频vs桂林seo顾问
文章目录
- 常用查找算法
- find
- find_if
- adjacent_find
- binary_search
- count
- count_if
常用查找算法
算法简介:
find//查找元素
find_if//按条件查找元素
adjacent_find//查找相邻重复元素
binary_search//二分查找法
count//统计元素个数
count_if//按条件统计元素个数
find
功能:查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()。
函数原型:
find(iterator beg,iterator end,value);
//beg开始迭代器
//end结束迭代器
//value查找的元素
#include<iostream>
using namespace std;
#include<vector>//查找内置数据类型
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找容器中是否有5这个元素vector<int>::iterator it = find(v.begin(), v.end(), 5);if (it == v.end()){cout << "没有找到!" << endl;}else{cout << "找到:" << *it << endl;}
}//查找自定义数据类型
class person
{
public:person(string name, int age){this->m_name = name;this->m_age = age;}//重载== 底层find知道如何对比person数据类型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 test02()
{vector<person> v;//创建数据person p1("aaa", 18);person p2("bbb", 19);person p3("ccc", 20);person p4("ddd", 21);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//查找p2这个人是否存在vector<person>::iterator it = find(v.begin(), v.end(), p2);if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到元素 姓名:" << it->m_name << " 年龄:" << it->m_age << endl;}
}int main()
{test01();test02();system("pause");return 0;
}
find_if
功能:按条件查找元素,找到返回指定元素的迭代器,找不到返回结束迭代器位置end()。
函数原型:
find_if(iterator beg,iterator end,_Pred);
//beg开始迭代器
//end结束迭代器
//_Pred函数或者谓词(返回bool类型的仿函数)
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>//查找内置数据类型
class greater5
{
public:bool operator()(int val){return val > 5;}
};void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找容器中是否有大于5的vector<int>::iterator it = find_if(v.begin(), v.end(), greater5());if (it == v.end()){cout << "没有找到!" << endl;}else{cout << "找到大于5的数为:" << *it << endl;//6}
}//查找自定义数据类型
class person
{
public:person(string name, int age){this->m_name = name;this->m_age = age;}string m_name;int m_age;
};class greater20
{
public:bool operator()(person& p){return p.m_age > 20;}
};void test02()
{vector<person> v;//创建数据person p1("aaa", 18);person p2("bbb", 19);person p3("ccc", 20);person p4("ddd", 21);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//查找容器中是否有年龄大于20的vector<person>::iterator it = find_if(v.begin(), v.end(), greater20());if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到元素 姓名:" << it->m_name << " 年龄:" << it->m_age << endl;}
}int main()
{test01();test02();system("pause");return 0;
}
adjacent_find
功能:查找相邻重复元素,返回相邻元素的第一个元素的迭代器。
函数原型:
adjacent_find(iterator beg,iterator end);
//beg开始迭代器
//end结束迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>void test01()
{vector<int> v;v.push_back(0);v.push_back(2);v.push_back(3);v.push_back(0);v.push_back(2);v.push_back(1);v.push_back(1);v.push_back(4);vector<int>::iterator pos = adjacent_find(v.begin(), v.end());if (pos == v.end()){cout << "未找到相邻重复元素" << endl;}else{cout << "找到相邻重复元素:" << *pos << endl;}
}int main()
{test01();system("pause");return 0;
}
binary_search
功能:查找指定元素是否存在,查到返回true,否则返回false,二分查找法查找效率很高。
注意:在无序序列中不可用。
函数原型:
bool binary_search(iterator beg,iterator value);
//beg开始迭代器
//end结束迭代器
//value查找的元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>//binary_search查找元素必须是有序序列
void test01()
{vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}v.push_back(5);//尾部插入5这个元素,变为无序序列,binary_search查找不到元素//查找容器中是否有9这个元素bool ret = binary_search(v.begin(), v.end(), 9);if (ret){cout << "找到元素" << endl;}else{cout << "未找到元素" << endl;}
}int main()
{test01();system("pause");return 0;
}
count
功能:统计元素个数。
函数原型:
count(iterator beg,iterator end,value);
//beg开始迭代器
//end结束迭代器
//value统计的元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>//统计内置数据类型
void test01()
{vector<int>v;v.push_back(1); v.push_back(2);v.push_back(5);v.push_back(7);v.push_back(8);v.push_back(2);v.push_back(5);v.push_back(2);//统计5的个数int num = count(v.begin(), v.end(), 5);cout << "5的个数为:" << num << 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;}}string m_name;int m_age;
};void test02()
{vector<person> v;//创建数据person p1("aaa", 18);person p2("bbb", 19);person p3("ccc", 18);person p4("ddd", 21);person p("ggg", 18);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//统计容器中与p年龄相同的人int num = count(v.begin(), v.end(), p);cout << "和ggg年龄相同的人数:" << num << endl;}int main()
{test01();test02();system("pause");return 0;
}
总结:统计自定义数据类型时,需要配合重载operator==。
count_if
功能:按提条件统计元素个数。
函数原型:
count_if(iterator beg,iterator end,_Pred);
//beg开始迭代器
//end结束迭代器
//_Pred谓词
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>//统计内置数据类型
class greater5
{
public:bool operator()(int val){return val > 5;}
};void test01()
{vector<int>v;v.push_back(1);v.push_back(2);v.push_back(5);v.push_back(7);v.push_back(8);v.push_back(2);v.push_back(5);v.push_back(2);//统计5的个数int num = count_if(v.begin(), v.end(), greater5());cout << "大于5的个数为:" << num << endl;
}//统计自定义数据类型
class person
{
public:person(string name, int age){this->m_name = name;this->m_age = age;}string m_name;int m_age;
};class greater20
{
public:bool operator()(const person& p){return p.m_age > 20;//如果年龄大于20返回真,否则返回假}
};void test02()
{vector<person> v;//创建数据person p1("aaa", 23);person p2("bbb", 19);person p3("ccc", 20);person p4("ddd", 21);person p("ggg", 18);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//统计容器中与p年龄相同的人int num = count_if(v.begin(), v.end(), greater20());cout << "年龄大于20的人数:" << num << endl;
}int main()
{test01();test02();system("pause");return 0;
}