1 find
功能描述:
- 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
函数原型:
-
find(iterator beg, iterator end, value);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
#include<iostream>
using namespace std;
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
/*- `find(iterator beg, iterator end, value); `
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素
*/
// 遍历函数
void Myprint1(int n) {
cout << n << " ";
}
// 查找内置数据类型
void test01() {
vector<int> v1;
v1.push_back(10);
v1.push_back(201);
v1.push_back(3);
v1.push_back(6);
v1.push_back(58);
v1.push_back(79);
cout << "查找内置数据类型" << endl;
cout << "容器v1里的元素如下:" << endl;
for_each(v1.begin(), v1.end(), Myprint1);
cout << endl << endl;
// 查找5这个元素是否存在
vector<int>::iterator it1=find(v1.begin(), v1.end(), 5);
// 查找6这个元素是否存在
vector<int>::iterator it2 = find(v1.begin(), v1.end(), 6);
if (it1 == v1.end())
{
cout << "容器v1里没有5这个元素!!" << endl << endl;
}
else {
cout << "已找到元素5!!" << endl;
cout << "它在第 " << (it1 - v1.begin()) + 1 << "个位置" << endl << endl;
}
if (it2 == v1.end())
{
cout << "容器v1里没有6这个元素!!" << endl << endl;
}
else {
cout << "已找到元素6!!" << endl;
cout << "它在第 " << (it2 - v1.begin()) + 1 << "个位置" << endl << endl;
}
cout << endl << endl;
}
// 查找自定义数据类型
class Person {
public:
Person(string name,int age):m_Age(age),m_Name(name){}
int m_Age;
string m_Name;
// 重载==
// 不然怎么比较两个对象是否相等呢
bool operator==(const Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
return false;
}
};
void Myprint2(Person p) {
cout << "姓名:" << p.m_Name << " "
<< "年龄:" << p.m_Age << endl;
}
void test02() {
cout << "查找自定义数据类型" << endl;
vector<Person> v2;
//创建数据
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("fff", 50);
v2.push_back(p1);
v2.push_back(p2);
v2.push_back(p3);
v2.push_back(p4);
cout << "容器v2里的元素如下:" << endl;
for_each(v2.begin(), v2.end(), Myprint2); cout << endl << endl;
// 查找p2是否在容器v2里
vector<Person>::iterator it1 = find(v2.begin(), v2.end(), p2);
// 查找p5是否在容器v2里
vector<Person>::iterator it2 = find(v2.begin(), v2.end(), p5);
if (it1 == v2.end())
{
cout << "容器v2里没有p2!!" << endl << endl;
}
else {
cout << "已找到p2!!" << endl;
cout << "它在第 " << (it1 - v2.begin()) + 1 << "个位置" << endl ;
cout << "它的姓名为:" << (*it1).m_Name << " "
<< "它的年龄为:" << it1->m_Age << endl << endl;
}
if (it2 == v2.end())
{
cout << "容器v2里没有p5!!" << endl << endl;
}
else {
cout << "已找到元素6!!" << endl;
//cout << "它在第 " << (it2 - v2.begin()) + 1 << "个位置" << endl ;
cout << "它的姓名为:" << (*it2).m_Name << " "
<< "它的年龄为:" << it2->m_Age << endl << endl;
}
cout << endl << endl;
}
int main() {
test01();
test02();
return 0;
}
注意查找自定义数据类型时,一定要重载==号。STL内置的函数底层实现时,不会识别自定义的数据类型。
// 查找自定义数据类型
class Person {
public:
Person(string name,int age):m_Age(age),m_Name(name){}
int m_Age;
string m_Name;
// 重载==
// 不然怎么比较两个对象是否相等呢
bool operator==(const Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
return false;
}
};
2 find_if
功能描述:
- 按条件查找元素
函数原型:
-
find_if(iterator beg, iterator end, _Pred);
按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
beg //开始迭代器
end //结束迭代器
_Pred //函数或者谓词(返回bool类型的仿函数)
#include<iostream>
using namespace std;
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
void Myprint1(int n) {
cout << n << " ";
}
class Person {
public:
Person(string name, int age) :m_Name(name), m_Age(age) {
}
// 重载=
bool operator=(Person& p) {
if (m_Name == p.m_Name && m_Age == p.m_Age) {
return 1;
}
return 0;
}
int m_Age;
string m_Name;
};
class MyFind1 {
public:
bool operator()(int a) {
return a > 10;
}
};
// 不一定要用谓词,普通函数也可以
/*bool Mycompare1(int a) {
return a > 10;
}*/
void test01() {
vector<int> v1;
v1.push_back(10);
v1.push_back(201);
v1.push_back(3);
v1.push_back(6);
v1.push_back(58);
v1.push_back(79);
cout << "查找内置数据类型" << endl;
cout << "容器v1里的元素如下:" << endl;
for_each(v1.begin(), v1.end(), Myprint1);
cout << endl << endl;
vector<int>::iterator it1 = find_if(v1.begin(), v1.end(),MyFind1());
// vector<int>::iterator it1 = find_if(v1.begin(), v1.end(), Mycompare1);
if (it1 == v1.end()) {
cout << "容器里没有大于10的数!!" << endl << endl;
}
else {
cout << "已找到一个容器里大于10的元素!!" << endl;
cout <<"它的值为:"<< * it1 << endl ;
cout << "它在容器里第" << (it1 - v1.begin()) + 1 << "个位置" << endl << endl;
}
cout << endl << endl;
}
void Myprint2(Person p) {
cout<<"姓名:"<<p.m_Name<<" "
<<"年龄:"<<p.m_Age<<endl;
}
class MyFind2 {
public:
bool operator()(Person p) {
return p.m_Age > 28;
}
};
// 不一定要用谓词,普通函数也可以
bool Mycompare2(Person p) {
return p.m_Age > 40;
}
// find_if查找自定义数据类型
void test02() {
cout << "查找自定义数据类型" << endl;
vector<Person> v2;
//创建数据
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("fff", 50);
v2.push_back(p1);
v2.push_back(p2);
v2.push_back(p3);
v2.push_back(p4);
cout << "容器v2里的元素如下:" << endl;
for_each(v2.begin(), v2.end(), Myprint2); cout << endl << endl;
vector<Person>::iterator it2 = find_if(v2.begin(), v2.end(), MyFind2());
if (it2 == v2.end()) {
cout << "容器v2里没有年龄大于28的对象!!" << endl << endl;
}
else {
cout << "已找到一个容器里年龄大于28的元素!!" << endl;
cout << "它的姓名为:" << it2->m_Name<<" "
<< "它的年龄为:"<<(*it2).m_Age<<endl;
cout << "它在容器里第" << (it2 - v2.begin()) + 1 << "个位置" << endl << endl;
}
cout << endl << endl;
vector<Person>::iterator it3 = find_if(v2.begin(), v2.end(), Mycompare2);
if (it3 == v2.end()) {
cout << "容器v2里没有年龄大于40的对象!!" << endl << endl;
}
else {
cout << "已找到一个容器里年龄大于40的元素!!" << endl;
cout << "它的姓名为:" << it3->m_Name << " "
<< "它的年龄为:" << (*it3).m_Age << endl;
cout << "它在容器里第" << (it3 - v2.begin()) + 1 << "个位置" << endl << endl;
}
cout << endl << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}
总结:find_if按条件查找使查找更加灵活,提供的仿函数可以改变不同的策略