0.前言
1.find
#include <iostream>
using namespace std;
// 常用查找算法 find
#include<vector>
#include<algorithm>
//查找 内置数据类型
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) //加const 防止修改数据
{
if (p.m_Name == this->m_Name && p.m_Age == this->m_Age)
{
return true;
}
else
{
return false;
}
}
string m_Name;
int m_Age;
};
void test02()
{
//创建数据
Person p1("a", 10);
Person p2("b", 20);
Person p3("c", 30);
//放入容器中
vector<Person>v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
Person p5("b", 20);
vector<Person>::iterator it = find(v.begin(), v.end(), p5);
if (it == v.end())
{
cout << "未找到" << endl;
}
else
{
cout << "找到: 姓名: " << it->m_Name << " age:" << (*it).m_Age << endl;
}
}
int main()
{
test01();
cout << "------------------------" << endl;
test02();
//cout << "------------------------" << endl << endl;
//test03();
//**************************************
system("pause");
return 0;
}
2.find_if
#include <iostream>
using namespace std;
// 常用查找算法 find_if
#include<vector>
//1.查找内置数据类型
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);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), Greater5());
if (it == v.end())
{
cout << "no find" << endl;
}
else
{
cout << "find element: " << (*it) << endl;
}
}
//2、查找自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
//方式一 利用仿函数
//class greater_Age20
//{
//public:
// bool operator()(const Person& p)
// {
// return p.m_Age > 20;
// }
//};
//方式二 利用普通函数
bool greater_Age20(const Person& p)
{
return p.m_Age > 20;
}
void test02()
{
//创建对象
Person p1("a", 10);
Person p2("b", 20);
Person p3("c", 30);
vector<Person>v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
//查找年龄大于20的人
vector<Person>::iterator it = find_if(v.begin(), v.end(), greater_Age20);
if (it == v.end())
{
cout << "no find" << endl;
}
else
{
cout << "find the element: name:" << it->m_Name << " age:" << it->m_Age << endl;
}
}
int main()
{
test01();
cout << "------------------------" << endl;
test02();
//cout << "------------------------" << endl << endl;
//test03();
//**************************************
system("pause");
return 0;
}
3.adjacent_find
#include <iostream>
using namespace std;
// 常用查找算法 adjacent_find
#include<vector>
#include<algorithm>
void test01()
{
//创建对象
vector<int>v;
v.push_back(1);
v.push_back(2);
v.push_back(1);
v.push_back(3);
v.push_back(4);
v.push_back(3);
v.push_back(3);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos == v.end())
{
cout << "no find adjacent duplcate elements " << endl;
}
else
{
cout << "find adjacent duplcate elements: " << *pos << endl;
}
}
int main()
{
test01();
cout << "------------------------" << endl;
//test02();
//cout << "------------------------" << endl << endl;
//test03();
//**************************************
system("pause");
return 0;
}
4.binary_search
#include <iostream>
using namespace std;
// 常用查找算法 binary_search 二分查找
#include<vector>
#include<algorithm>
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 << "find point element" << endl;
}
else
{
cout << "no find" << endl;
}
}
int main()
{
test01();
cout << "------------------------" << endl;
//test02();
//cout << "------------------------" << endl << endl;
//test03();
//**************************************
system("pause");
return 0;
}
5.count
#include <iostream>
using namespace std;
// 常用查找算法 count
#include<vector>
#include<algorithm>
//1.统计内置数据类型
void test01()
{
vector<int>v;
v.push_back(1);
v.push_back(4);
v.push_back(1);
v.push_back(3);
int num = count(v.begin(), v.end(), 1);
cout << "the nmber of point element: " << num << endl;
}
//2.统计自定义数据类型
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()
{
//创建对象
Person p1("刘备", 35);
Person p2("薇恩", 24);
Person p3("皮城", 35);
Person p4("光辉", 40);
vector<Person>v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
Person p5("卡米尔", 35);
int num = count(v.begin(), v.end(), p5);
cout << "跟卡米尔同岁的人有多少个: " << num << endl;
}
int main()
{
test01();
cout << "------------------------" << endl;
//test02();
//cout << "------------------------" << endl << endl;
//test03();
//**************************************
system("pause");
return 0;
}
6.count_if
#include <iostream>
using namespace std;
// 常用查找算法 count_if
#include<vector>
#include<algorithm>
//1、统计内置数据类型
//利用仿函数
class Greater3
{
public:
bool operator()(int val)
{
return val > 3;
}
};
void test01()
{
vector<int>v;
v.push_back(1);
v.push_back(6);
v.push_back(3);
v.push_back(2);
v.push_back(4);
v.push_back(3);
int num = count_if(v.begin(), v.end(), Greater3());
cout << "the number of element greater than 3 : " << num << endl;
}
//2、统计自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
//利用普通函数
bool age_Greater35(const Person& p)
{
if (p.m_Age > 35)
{
return true;
}
else
{
return false;
}
}
void test02()
{
//创建对象
Person p1("刘备", 35);
Person p2("薇恩", 24);
Person p3("皮城", 35);
Person p4("光辉", 40);
vector<Person>v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//统计岁数大于35的人物的个数
int num = count_if(v.begin(), v.end(), age_Greater35);
cout << "the number of character whose age greater than 35 :" << num << endl;
}
int main()
{
test01();
cout << "------------------------" << endl;
test02();
//cout << "------------------------" << endl << endl;
//test03();
//**************************************
system("pause");
return 0;
}