按值查找
find(iterator beg, iterator end, value)
- find算法 查找元素
- @param beg 容器开始迭代器
- @param end 容器结束迭代器
- @param value 查找的元素
- @return 返回查找元素的迭代器的位置
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #include <algorithm> #include <vector> #include <string> void test01() { vector<int>v; for (int i = 0; i < 10; i++) { v.push_back(i); } vector<int>::iterator pos = find(v.begin(), v.end(), 5); if (pos != v.end()) { cout << "找到了pos:" << *pos << endl; } else { cout << "未找到" << endl; } } //自定义类型查找 class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } bool operator==(const Person & p)const //需要const修饰 不允许修改 { if (p.m_Name == this->m_Name && p.m_Age == this->m_Age) { return true; } return false; } string m_Name; int m_Age; }; void test02() { vector<Person>v; Person p1("悟空", 20); Person p2("悟能", 19); Person p3("悟净", 18); Person p4("悟世", 17); v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); Person pp("悟净", 18); vector<Person>::iterator pos = find(v.begin(), v.end(), pp); if (pos != v.end()) { cout << "找到姓名:" << (*pos).m_Name << " 年龄:" << pos->m_Age << endl; } else { cout << "未找到" << endl; } } int main() { //test01(); test02(); system("Pause"); return 0; }
结果:
按条件查找
find_if(iterator beg, iterator end, _callback);
- find_if算法 条件查找
- @param beg 容器开始迭代器
- @param end 容器结束迭代器
- @param callback 回调函数或者谓词(返回bool类型的函数对象)
- @return bool 查找返回true 否则false
class myCompare:public binary_function<Person*,Person*,bool> { public: bool operator()(Person * p1, Person *p2) const { if (p1->m_Age == p2->m_Age && p1->m_Name == p2->m_Name) //if (p1.m_Age == p2.m_Age && p1.m_Name == p2.m_Name) { return true; } return false; } }; void test03() { vector<Person*>v; Person p1("悟空", 20); Person p2("悟能", 19); Person p3("悟净", 18); Person p4("悟世", 17); v.push_back(&p1); v.push_back(&p2); v.push_back(&p3); v.push_back(&p4); Person* p = new Person("悟世", 17); vector<Person*>::iterator pos = find_if(v.begin(), v.end(), bind2nd(myCompare(), p)); if (pos != v.end()) { cout << "找到姓名:" << (*pos)->m_Name << " 年龄:" << (*pos)->m_Age << endl; } else { cout << "未找到" << endl; } }
结果:
查找相邻重复元素
adjacent_find(iterator beg, iterator end, _callback);
- adjacent_find算法 查找相邻重复元素
- @param beg 容器开始迭代器
- @param end 容器结束迭代器
- @param _callback 回调函数或者谓词(返回bool类型的函数对象)
- @return 返回相邻元素的第一个位置的迭代器
void test04() { vector<int>v; v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(4); v.push_back(5); v.push_back(2); vector<int>::iterator pos = adjacent_find(v.begin(), v.end()); //查找相邻的重复数字,不相邻的不算 if (pos != v.end()) { cout << "找到重复数字:" << *pos << endl; } else { cout << "未找到" << endl; } }
结果:
二分法查找值
注意:容器必须是有序的
bool binary_search(iterator beg, iterator end, value);
- binary_search算法 二分查找法
- 注意: 在无序序列中不可用
- @param beg 容器开始迭代器
- @param end 容器结束迭代器
- @param value 查找的元素
- @return bool 查找返回true 否则false
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); //返回bool if (ret) { cout << "找到了数字:4" << endl; } else { cout << "未找到4" << endl; } }
结果:
按值统计元素出现的次数
count(iterator beg, iterator end, value);
- count算法 统计元素出现次数
- @param beg 容器开始迭代器
- @param end 容器结束迭代器
- @param value回调函数或者谓词(返回bool类型的函数对象)
- @return int返回元素个数
按条件统计元素出现的次数
count_if(iterator beg, iterator end, _callback);
- count算法 统计元素出现次数
- @param beg 容器开始迭代器
- @param end 容器结束迭代器
- @param callback 回调函数或者谓词(返回bool类型的函数对象)
- @return int返回元素个数
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); v.push_back(4); int _total = count(v.begin(), v.end(), 4); cout << "4出现的次数为:" << _total << endl; int greatThen_5 = count_if(v.begin(), v.end(), bind2nd(greater<int>(), 5)); //6 7 8 9 cout << "大于5的数量为:" << greatThen_5 << endl; //4 }
结果: