常用查找算法
算法简介
![在这里插入图片描述](https://img-blog.csdnimg.cn/55854575a84041c893e16c14d624f2a0.png)
find
功能描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/693cce6bb970449fa1506f839019385b.png)
函数原型
![在这里插入图片描述](https://img-blog.csdnimg.cn/4fa96290c3a84c4dbd30cc41beafb6da.png)
示例
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
void test() {
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 << "找到了!" << endl;
}
}
class Person {
public:
Person(string name, int age) {
this->age = age;
this->name = name;
}
bool operator==(const Person &p) {
if (this->name == p.name && this->age == p.age) {
return true;
}
return false;
}
string name;
int age;
};
void test02() {
vector<Person> v;
Person p1("aaa", 19);
Person p2("bbb", 18);
Person p3("ccc", 20);
Person p4("ddd", 16);
Person p5("eee", 23);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
vector<Person>::iterator it = find(v.begin(), v.end(), p3);
if (it == v.end()) {
cout << "没有找到!" << endl;
} else {
cout << "找到了!" << endl;
}
}
int main() {
test();
test02();
return 0;
}
find_if
功能描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/5b919b0690884db381902d6dfc0bc422.png)
函数原型
![在这里插入图片描述](https://img-blog.csdnimg.cn/c87e9adb9f1b4579a9d7c046ee75d703.png)
示例
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
class GreaterFive {
public:
bool operator()(int val) {
return val > 5;
}
};
void test() {
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if (it == v.end()) {
cout << "没有找到!" << endl;
} else {
cout << "找到了!" << endl;
}
}
class Person {
public:
Person(string name, int age) {
this->age = age;
this->name = name;
}
bool operator==(const Person &p) {
if (this->name == p.name && this->age == p.age) {
return true;
}
return false;
}
string name;
int age;
};
class Greater20{
public:
bool operator()(Person &p){
return p.age > 20;
}
};
void test02() {
vector<Person> v;
Person p1("aaa", 19);
Person p2("bbb", 18);
Person p3("ccc", 20);
Person p4("ddd", 16);
Person p5("eee", 23);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
vector<Person>::iterator it = find_if(v.begin(),v.end(),Greater20());
if (it == v.end()) {
cout << "没有找到!" << endl;
} else {
cout << "找到了!" << endl;
}
}
int main() {
test();
test02();
return 0;
}
adjacent_find
功能描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/80402faa8b194169aa343e4816abdedc.png)
函数原型
![在这里插入图片描述](https://img-blog.csdnimg.cn/13fe06935b354e3f85e8f4a8d70c998f.png)
示例
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
void test() {
vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(0);
v.push_back(2);
v.push_back(2);
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() {
test();
return 0;
}
binary_search
功能描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/1f517f38c302494d9466d027153ab7dd.png)
函数原型
![在这里插入图片描述](https://img-blog.csdnimg.cn/0c52900c38334918b6d4005b08b0de0d.png)
示例
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
void test() {
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
bool result = binary_search(v.begin(),v.end(),9);
if (result) {
cout << "有9" << endl;
} else {
cout << "没有9" << endl;
}
}
int main() {
test();
return 0;
}
cout
功能描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/6986fe37b189421dbce76da17fda62b9.png)
函数原型
![在这里插入图片描述](https://img-blog.csdnimg.cn/2f54c0d7d1344a789f54a0ab62b9ca5a.png)
示例
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
void test() {
vector<int> v;
v.push_back(10);
v.push_back(10);
v.push_back(4);
v.push_back(6);
v.push_back(2);
v.push_back(3);
v.push_back(3);
int result = count(v.begin(), v.end(), 10);
cout << "10的元素个数为:" << result << endl;
}
class Person {
public:
Person(string name, int age) {
this->age = age;
this->name = name;
}
bool operator==(const Person &p) {
if (this->age == p.age) {
return true;
}
return false;
}
string name;
int age;
};
void test2() {
vector<Person> v;
Person p1("刘备", 18);
Person p2("关羽", 19);
Person p3("刘禅", 10);
Person p4("赵云", 18);
Person p5("张飞", 85);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person pTest("诸葛亮", 18);
int result = count(v.begin(), v.end(), pTest);
cout << "与诸葛亮年龄一样的有" << result << "个" << endl;
}
int main() {
test();
return 0;
}
count_if
功能描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/94694f0dc12e4c20a28e94c6426772d8.png)
函数原型
![在这里插入图片描述](https://img-blog.csdnimg.cn/ac554f9ffe734fc0a4260869a767fd64.png)
示例
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
class GreaterFive {
public:
bool operator()(int val) {
return val > 5;
}
};
void test() {
vector<int> v;
v.push_back(10);
v.push_back(10);
v.push_back(4);
v.push_back(6);
v.push_back(2);
v.push_back(3);
v.push_back(3);
int result = count_if(v.begin(), v.end(), GreaterFive());
cout << "大于5的元素个数为:" << result << endl;
}
class Person {
public:
Person(string name, int age) {
this->age = age;
this->name = name;
}
bool operator==(const Person &p) {
if (this->age == p.age) {
return true;
}
return false;
}
string name;
int age;
};
class Greater20{
public:
bool operator()(const Person &p){
return p.age > 20;
}
};
void test2() {
vector<Person> v;
Person p1("刘备", 18);
Person p2("关羽", 19);
Person p3("刘禅", 10);
Person p4("赵云", 18);
Person p5("张飞", 85);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int result = count_if(v.begin(), v.end(), Greater20());
cout << "年龄大于20的元素个数有" << result << "个" << endl;
}
int main() {
test();
test2();
return 0;
}