1.1 find 按值查找
1.2 find_if 按条件进行查找
1.3 adjacent_find算法 查找相邻重复元素
1.4 binary_search算法 二分查找法
1.4.1 有序序列
1.5 count 按值统计
1.6 count_if 按条件统计
find
void test01() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(rand() % 100);
}
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
cout << endl;
vector<int>::iterator pos = find(v.begin(), v.end(), 5);
if (pos != v.end()) {
cout << "找到了,值为" << *pos << endl;
}
else {
cout << "未找到" << endl;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a45f080e69e3b22da4e6383508e671b7.png)
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
}
bool operator==(const Person&p) {
if (this->m_age == p.m_age&&this->m_name == p.m_name) {
return true;
}
return false;
}
string m_name;
int m_age;
};
void test02() {
vector<Person>v;
Person p1("渣渣猫", 26);
Person p2("土拨鼠", 25);
Person p3("波波", 19);
Person p4("锯嘴葫芦", 18);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator pos = find(v.begin(), v.end(), p2);
if (pos != v.end()) {
cout << "找到了,姓名为:" << (*pos).m_name << " 年龄是:" << pos->m_age << endl;
}
else {
cout << "未找到" << endl;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fc42610ca51d6bbc940ce625606fb6d0.png)
find_if
class MyCompar :public binary_function<Person*,Person*,bool>{
public:
bool operator()(Person *p1, Person *p2) const{
if (p1->m_name == p2->m_name&&p1->m_age == p2->m_age) {
return true;
}
return false;
}
};
void test03() {
vector<Person*>v;
Person p1("渣渣猫", 26);
Person p2("土拨鼠", 25);
Person p3("波波", 19);
Person p4("锯嘴葫芦", 18);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
Person *p = new Person("Nick", 30);
vector<Person*>::iterator pos = find_if(v.begin(), v.end(), bind2nd(MyCompar(),p));
if (pos != v.end()) {
cout << "找到了,姓名为:" << (*pos)->m_name << " 年龄是:" << (*pos)->m_age << endl;
}
else {
cout << "未找到" << endl;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/920963c36ee727d58bef3c4b9a5fea33.png)
adjacent_find
void test04() {
vector<int>v;
v.push_back(5);
v.push_back(15);
v.push_back(25);
v.push_back(25);
v.push_back(25);
v.push_back(35);
v.push_back(35);
v.push_back(55);
v.push_back(5);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos != v.end()) {
cout << "相邻的重复元素为" << *pos << endl;
}
else {
cout << "未找到" << endl;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7c0093f8a5ffdd9cd94bce5f635ccb95.png)
binary_search
void test05() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
bool ret = binary_search(v.begin(), v.end(), 4);
if (ret) {
cout << "找到了4" << endl;
}
else {
cout << "未找到" << endl;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5271522e4df802d78ee313648b5cfec0.png)
count
count_if
class LargeThanFour {
public:
bool operator()(int val) {
return val > 4;
}
};
void test06() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
v.push_back(4);
v.push_back(4);
v.push_back(4);
int num = count(v.begin(), v.end(), 4);
cout << "4的个数为:" << num << endl;
int num1 = count_if(v.begin(), v.end(), LargeThanFour());
cout << "大于4的个数为:" << num1 << endl;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9e28231ad00fe9b5f0d164dcb98108b9.png)