Loading... ## 算法简介: <div class="tip inlineBlock success"> find;//查找元素 find_if;//按条件查找元素 adjacent_find;//查找相邻重复元素 binary_search;//二分查找法 count;//统计元素个数 count_if;//按条件统计元素个数 </div> ## find ### 功能描述: 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end() ### 函数原型: find(iterator beg, iterator end, value); //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置 //beg开始迭代器 //end结束迭代器 //value查找的元素 ### 示例: ```cpp #include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } public: string m_Name; int m_Age; bool operator == (const Person &p1)//重载 == 底层find 知道如何对比Person数据类型 { if (this->m_Age == p1.m_Age && this->m_Name == p1.m_Name) { return true; } else { return false; } } }; void text2() { vector<Person>v1; Person p1("小明", 8); Person p2("小红", 6); Person p3("小王", 9); Person p4("小民", 10); Person p5("小李", 2); v1.push_back(p1); v1.push_back(p2); v1.push_back(p3); v1.push_back(p4); v1.push_back(p5); Person pp("小民", 10); vector<Person>::iterator it = find(v1.begin(), v1.end(), pp); if (it == v1.end()) { cout << "没有找到该元素!" << endl; } else { cout << "找到名字为:" << it->m_Name << " 年龄为:" << it->m_Age <<" 的小盆友!"<< endl; } } void text1() { vector<int>v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } vector<int>::iterator it = find(v1.begin(), v1.end(), 5); if (it == v1.end()) { cout << "没有找到指定元素!" << endl; } else { cout << "找到:" << *it << endl; } } int main() { text1(); text2(); return 0; } ``` ## find_if ### 功能描述: 按条件查找元素 ### 函数原型: find_if(iterator beg, iterator end, _pred) //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置 //beg开始迭代器 //end结束迭代器 //_pred函数或者谓词(返回bool类型的仿函数) ### 示例: ```cpp #include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } public: string m_Name; int m_Age; }; class Person1 { public: bool operator()(int &num)const { return num > 6; } }; class Person2 { public: bool operator()(Person &p1)const { return p1.m_Age > 6; } }; void text2()//自定义数据类型 { vector<Person>v1; Person p1("小明", 8); Person p2("小红", 6); Person p3("小王", 9); Person p4("小民", 10); Person p5("小李", 2); v1.push_back(p1); v1.push_back(p2); v1.push_back(p3); v1.push_back(p4); v1.push_back(p5); vector<Person>::iterator it = find_if(v1.begin(), v1.end(), Person2()); if (it == v1.end()) { cout << "没有找到该元素!" << endl; } else { cout << "找到名字为:" << it->m_Name << " 年龄为:" << it->m_Age <<" 的小盆友!"<< endl; } } void text1()//内置数据类型 { vector<int>v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } vector<int>::iterator it = find_if(v1.begin(), v1.end(), Person1()); if (it == v1.end()) { cout << "没有找到指定元素!" << endl; } else { cout << "找到:" << *it << endl; } } int main() { text1(); text2(); return 0; } ``` ## adjacent_find ### 功能描述: 查找相邻重复元素 ### 函数原型: adjacent_find(iterator beg, iterator end); //查找相邻重复元素,返回相邻元素的第一个位置的迭代器 //beg开始迭代器 //end结束迭代器 ### 示例: ```cpp #include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; void text1()//内置数据类型 { vector<int>v1; v1.push_back(0); v1.push_back(1); v1.push_back(2); v1.push_back(2); v1.push_back(0); vector<int>::iterator it = adjacent_find(v1.begin(), v1.end()); if (it == v1.end()) { cout << "没有找到相邻元素!" << endl; } else { cout << "找到相邻元素:" << *it << endl; } } int main() { text1(); return 0; } ``` ## binary_search ### 功能描述: 查找指定元素是否存在 ### 函数原型: bool binary_search(iterator beg, iterator end, value); //查找指定的元素,查到返回true,否则返回false //注意:在无序序列不可用 //beg开始迭代器 //end结束迭代器 //value查找的元素 ### 示例: ```cpp #include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; void text1()//内置数据类型 { vector<int>v1; for (int i = 0; i < 10; i++) { v1.push_back(i); } bool rel = binary_search(v1.begin(), v1.end(), 7); if (rel) { cout << "找到元素!" << endl; } else { cout << "没有找到元素!" << endl; } } int main() { text1(); return 0; } ``` ## count ### 功能描述: 统计元素个数 ### 函数原型: count(iterator beg, iterator end, value) //统计元素出现次数 //beg开始迭代器 //end结束迭代器 //value统计的元素 ### 示例: ```cpp #include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } bool operator == (const Person p1) { if (p1.m_Age == this->m_Age) { return true; } else { return false; } } string m_Name; int m_Age; }; void text2()//自定义数据类型 { vector<Person>p; Person p1("小民", 10); Person p2("小望", 20); Person p3("小李", 10); Person p4("小杨", 30); Person p5("小红", 10); Person p6("小美", 10); p.push_back(p1); p.push_back(p2); p.push_back(p3); p.push_back(p4); p.push_back(p5); int sum = count(p.begin(), p.end(), p5); cout << "年龄为10的人数为:" << sum << endl; } void text1()//内置数据类型 { vector<int>v1; v1.push_back(0); v1.push_back(0); v1.push_back(1); v1.push_back(0); v1.push_back(1); v1.push_back(6); int sum = count(v1.begin(), v1.end(), 0); cout << "0的个数为:" << sum<<endl; } int main() { text1(); text2(); return 0; } ``` ## count_if ### 功能描述: 按条件统计元素个数 ### 函数原型: count_if(iterator beg, iterator end, _pred); //按条件统计元素出现次数 //beg开始迭代器 //end结束迭代器 //_pred谓词 ### 示例: ```cpp #include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } bool operator == (const Person p1) { if (p1.m_Age == this->m_Age) { return true; } else { return false; } } string m_Name; int m_Age; }; class Greater_than_five_0 { public: bool operator ()(const int val) { return val > 5; } }; class Greater_than_five_1 { public: bool operator()(const Person p1) { return p1.m_Age > 5; } }; void text2()//自定义数据类型 { vector<Person>p; Person p1("小民", 10); Person p2("小望", 20); Person p3("小李", 10); Person p4("小杨", 30); Person p5("小红", 10); p.push_back(p1); p.push_back(p2); p.push_back(p3); p.push_back(p4); p.push_back(p5); int sum = count_if(p.begin(), p.end(), Greater_than_five_1()); cout << "大于5的个数为:" << sum << endl; } void text1()//内置数据类型 { vector<int>v1; v1.push_back(0); v1.push_back(6); v1.push_back(1); v1.push_back(8); v1.push_back(1); v1.push_back(6); int sum = count_if(v1.begin(), v1.end(), Greater_than_five_0()); cout << "大于5的个数为:" << sum<<endl; } int main() { text1(); text2(); return 0; } ``` 最后修改:2021 年 09 月 24 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果文章有用,请随意打赏。