#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
using namespace std;
//find 查找元素,内置类型
void test01()
{
vector<int>v = { 1,2,3,4,5,6,7,8,9 };
//查找在区间内出现的第一个5,返回当前查找元素迭代器
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if (it != v.end())
{
cout << "找到!" << endl;
}
else
{
cout << "未找到!" << endl;
}
}
//find 查找元素,自定义类型,得告诉编译器如何做对比
class Person
{
public:
Person(string name, int age)
{
m_name = name;
m_age = age;
}
bool operator==(const Person &p)
{
if (this->m_age == p.m_age&&this->m_name == p.m_name)
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
void test02()
{
vector<Person>v;
//创建数据
Person p1("王大锤", 18);
Person p2("喵喵喵", 20);
Person p3("嘤嘤嘤", 19);
//插入数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
vector<Person>::iterator it = find(v.begin(), v.end(), p2);
if (it != v.end())
{
cout << "找到:姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
}
else
{
cout << "未找到!" << endl;
}
}
//find_if 按条件查找-----------------------------------------------
//find_if 查找元素,内置类型
class GreaterFive
{
public:
bool operator()(int val)
{
return val>5;
}
};
void test03()
{
vector<int>v = { 1,2,3,4,5,6,7,8,9 };
//查找在区间内出现的第一个5,返回当前查找元素迭代器
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if (it != v.end())
{
cout << "找到!" << endl;
for (; it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
else
{
cout << "未找到!" << endl;
}
}
//find_if 自定义数据类型
//一元谓词
class GreaterAge
{
public:
bool operator()(Person &p)
{
return p.m_age > 19;
}
};
void test04()
{
vector<Person>v;
//创建数据
Person p1("王大锤", 18);
Person p2("喵喵喵", 20);
Person p3("嘤嘤嘤", 19);
//插入数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
vector<Person>::iterator it = find_if(v.begin(), v.end(),GreaterAge());
if (it != v.end())
{
cout << "找到年龄大于19的人:姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
}
else
{
cout << "未找到!" << endl;
}
}
//adjacent_find 查找相邻重复元素
void test05()
{
vector<int>v = { 0,1,1,2,3,3,4,4,5,5,6,7,8,8 ,1};
int count = 0;
while (1)
{
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if (it != v.end())
{
v.erase(it);
count++;
}
else
{
break;
}
}
cout << "已删除:" << count << "个相邻的重复元素" << endl;
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
}
//binary_search 查找指定元素是否存在,存在返回true,不存在返回false
//在无序序列中不可用,二分查找法
void test06()
{
vector<int>v = { 0,1,2,3,4,5,6,7,8,9 };
if (binary_search(v.begin(), v.end(), 6))
{
cout << "元素6存在" << endl;
}
else
{
cout << "该元素不存在" << endl;
}
}
//count 统计元素个数
void test07()
{
vector<int>v = { 0,1,2,2,2,2,3,4,5,6,7,8,9 };
int num1 = count(v.begin(), v.end(), 2);
cout << "内置类型的个数为:" << num1 << endl;
//需重载==运算符
vector<Person>p;
Person p1("王大锤", 18);
Person p4("王大锤", 18);
Person p2("喵喵喵", 20);
Person p3("嘤嘤嘤", 19);
p.push_back(p1);
p.push_back(p2);
p.push_back(p3);
p.push_back(p4);
Person p5("王大锤", 18);
int num2 = count(p.begin(), p.end(), p5);
cout << "自定数据类型:" << num2 << endl;
}
//count_if 按条件统计元素个数
class GreatTwo
{
public:
bool operator()(int val)
{
return val > 2;
}
};
void test08()
{
vector<int>v = { 0,1,2,3,4,5,6,7,8,9 };
int num = count_if(v.begin(), v.end(),GreatTwo());
cout << "大于2的数有:" << num << "个" << endl;
//需调用一元谓词
vector<Person>p;
Person p1("王大锤", 18);
Person p2("喵喵喵", 20);
Person p3("嘤嘤嘤", 19);
Person p4("王小锤", 21);
p.push_back(p1);
p.push_back(p2);
p.push_back(p3);
p.push_back(p4);
int num2 = count_if(p.begin(), p.end(), GreaterAge());
cout << "自定数据类型:" << num2 << endl;
}
int main()
{
test08();
system("pause");
return 0;
}
C++:STL-常用查找算法
最新推荐文章于 2023-05-04 22:07:23 发布