算法简介:
1.find //查找元素
2.find_if //按条件查找元素
3.adjacent_find //查找相邻重复元素
4.binary_search //二分查找法 存在返回真 否则假
5.count //统计元素个数
6.count_if //按条件统计元素个数
1.find
功能描述
查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
函数原型:
find(iterator beg, iterator end, value);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始选代器
// end 结束选代器
// value 查找的元素
//查找 内置数据类型
//查找 容器中 是否有5个这个元素
vector<int>::iterator it=find(v.begin(), v.end(), 5);
if (it == v.end())
{}
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;
};
//查找 自定义数据类型
{
Person pp("bbb", 20);
vector<Person>::iterator it = find(v.begin(), v.end(), pp);
}
总结: 利用find可以在容器中找指定的元素,返回值是迭代器
2.find_if
功能描述:
按条件查找元素
函数原型:
find_if(iterator beg, iterator end,_Pred);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始选代器
// end 结束选代器
//_Pred 函数或者谓词 (返回bool类型的仿函数)
//1.查找内置数据类型
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
//2.查找自定义数据类型
class Greater20
{
public:
bool operator()(Person& p)
{
return p.m_Age > 20;
}
};
//找到年龄大于20的人
vector<Person>::iterator it=find_if(v.begin(), v.end(), Greater20());
3.adjacent_find
功能描述:
查找相邻重复元素
函数原型:
adjacent_find(iterator beg, iterator end); 较少用到,但是一些题目可能会出现
// 查找相邻重复元素,返回相邻元素的第一个位置的迭代器 如果间接重复是查不到的
// beg 开始选代器
// 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(3);
vector<int>::iterator pos=adjacent_find(v.begin(), v.end());
//在这里他找到的是3,需要得到3时需要*pos
}
4.binary_search
功能描述:
查找指定元素是否存在
函数原型:
bool binary_search(iterator beg, iterator end, value);
// 查找指定的元素,查到 返回true 否则false
// 注意:在无序序列中不可用
// beg 开始选代器
// end 结束选代器 // value 查找的元素
//如果是无序序列,结果未知
//查找容器中是否有9元素
//注意:容器必须是有序的序列
bool ret= binary_search(v.begin(), v.end(), 9);
总结:二分查找法查找效率很高,值得注意的是查找的容器中元素必须的有序序列
5.count
功能描述:
统计元素个数,返回的上一个整数
函数原型:
count(iterator beg, iterator end, value);
// 统计元素出现次数
// beg 开始选代器
// end 结束选代器
// value 统计的元素
//1.统计内置数据类型
int mun = count(v.begin(), v.end(), 40);//查找40出现的次数
//2.统计自定义数据类型
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("刘备", 35);
Person p3("张飞", 35);
//将人员插入到容器中
v.push_back(p1);
v.push_back(p2);
Person p("诸葛亮", 35);
//查找和诸葛亮同岁数的人数
int num = count(v.begin(), v.end(), p);
}
总结: 统计自定义数据类型时候,需要配合重载 operator==
6.count_if
功能描述:
按条件统计元素个数
函数原型:
count_if(iterator beg, iterator end,_Pred);
// 按条件统计元素出现次数
// beg 开始选代器
// end 结束选代器
//_Pred 谓词
//统计内置数据类型
class Greater20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
//大于20的元素个数
int num = count_if(v.begin(), v.end(), Greater20());
//统计自定义数据类型
class AgeGreater20
{
public:
bool operator()(const Person& p)
{
return p.m_Age > 20;
}
};
//统计 大于20岁人员个数
int num = count_if(v.begin(), v.end(), AgeGreater20());
仅个人看视频笔记与理解,如有误可指出谢谢