算法简介:
函数原型:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//find——查找算法
//查找内置数据类型
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 << "没有找到!" << 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", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("eee", 50);
//导入数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person pp("ccc", 30);
vector<Person>::iterator it = find(v.begin(), v.end(), pp);
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到了:name = " << (*it).m_name << " age = " << it->m_age << endl;
}
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
函数原型:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//find_if——查找算法
class GreatFive
{
public:
bool operator ()(int val)
{
if (val > 5)
{
return true;
}
else
{
return false;
}
}
};
//查找内置数据类型
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreatFive());
if (it == v.end())
{
cout << "没有找到大于5的数:" << endl;
}
else
{
cout << "找到了:" << *it << endl;
}
}
//查找自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
class Great20
{
public:
bool operator()(Person p)
{
return p.m_age > 20;
}
};
void test02()
{
vector<Person>v;
//创建数据
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
//插入数据
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(), Great20());
if (it == v.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到了:name = " << it->m_name << " age = " << it->m_age << endl;
}
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
函数原型:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//adjacent_find——查找相邻重复元素
void test01()
{
vector<int>v;
v.push_back(1);
v.push_back(5);
v.push_back(3);
v.push_back(9);
v.push_back(6);
v.push_back(6);
v.push_back(8);
v.push_back(3);
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;
}
函数原型:
#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(2); 如果是无序序列,结果未知!
//查找容器中是否有9元素
//注意:容器必须是有序序列
bool ret = binary_search(v.begin(), v.end(), 9);
if (ret)
{
cout << "找到了!" << endl;
}
else
{
cout << "没有找到!" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
注:二分查找法效率很高,但查找容器中元素必须为有序序列。
函数原型:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//count——查找算法
//查找内置数据类型
void test01()
{
vector<int>v;
v.push_back(20);
v.push_back(50);
v.push_back(90);
v.push_back(40);
v.push_back(50);
v.push_back(80);
v.push_back(50);
int num = count(v.begin(), v.end(), 50);
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 (p.m_age == this->m_age)
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
void test02()
{
vector<Person>v;
//创建数据
Person p1("aaa", 25);
Person p2("bbb", 45);
Person p3("ccc", 25);
Person p4("ddd", 60);
Person p5("eee", 25);
Person p6("fff", 16);
Person p7("ggg", 25);
//导入数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
v.push_back(p6);
v.push_back(p7);
Person c("compare", 25);
int num = count(v.begin(), v.end(), c);
cout << "与compare年龄相同的人个数:" << num << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
注:统计自定义数据类型时需要配合重载operator==
函数原型:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
//count_if——条件查找
//查找内置数据类型
class Greater30
{
public:
bool operator()(int val)
{
return val > 30;
}
};
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(50);
v.push_back(80);
v.push_back(60);
v.push_back(30);
v.push_back(40);
//查找大于30的数的个数
int num = count_if(v.begin(), v.end(), Greater30());
cout << "大于30的数的个数:" << 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 ageGreater18
{
public:
bool operator()(Person& p)
{
return p.m_age > 18;
}
};
void test02()
{
vector<Person>v;
//创建数据
Person p1("aaa", 15);
Person p2("bbb", 45);
Person p3("ddd", 18);
Person p4("fff", 60);
Person p5("jjj", 85);
//导入数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//查找年龄大于18的人的个数
int num = count_if(v.begin(), v.end(), ageGreater18());
cout << "年龄大于18的人数:" << num << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
?