查找算法
约 805 字大约 3 分钟...
::: tips 查找算法 :::
find_if(iterator beg, iterator end, _Pred);
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<string> //常用查找算法:find_if //1. 查找内置数据类型 class GreaterFive { public: bool operator()(int val) { return val > 5; } }; void test01() { vector<int>v; for (int i = 0; i < 10; i++) { v.push_back(i); } //查找容器中是否有大于5的元素 vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive()); if (it == v.end()) { cout << "未找到大于5的元素!" << endl; } else { cout << "找到大于5的元素:" << *it << endl; } } //2. 查找自定义数据类型(必须重载==) class Person { public: Person(string name, int age) { this->m_Name = name; this->m_Age = age; } //重载==使得底层find知道如何对比Person数据类型 bool operator==(const Person &p) { if (this->m_Name == p.m_Name&&this->m_Age == p.m_Age) { return true; } else { return false; } } string m_Name; int m_Age; }; class Greater20 { public: bool operator()(Person &p) { return p.m_Age > 20; } }; void test02() { vector<Person>v; //创建数据 Person p1("aaa", 10); Person p2("bbb", 20); Person p3("ccc", 30); Person p4("ddd", 40); //放到容器中 v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); //查找容器中是否有年龄大于20的人 vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20()); if (it == v.end()) { cout << "未找到年龄大于20的人!" << endl; } else { cout << "找到年龄大于20的人!姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl; } } int main() { test01(); test02(); system("pause"); return 0; }
adjacent_find(iterator beg, iterator end);
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<string> //常用查找算法:adjacent_find void test01() { vector<int>v; v.push_back(0); v.push_back(2); v.push_back(0); v.push_back(3); v.push_back(1); v.push_back(4); v.push_back(3); v.push_back(3); //查找容器中是否有相邻重复元素 vector<int>::iterator it = adjacent_find(v.begin(), v.end()); if (it == v.end()) { cout << "未找到相邻重复元素!" << endl; } else { cout << "找到相邻重复元素:" << *it << endl; } } int main() { test01(); system("pause"); return 0; }
count_if(iterator beg, iterator end, _Pred);
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<string> //常用查找算法:count_if //1. 统计内置数据类型 class Greater20 { public: bool operator()(int val) { return val > 20; } }; void test01() { vector<int>v; v.push_back(10); v.push_back(40); v.push_back(30); v.push_back(40); v.push_back(20); v.push_back(40); //统计大于20的元素的个数 int num = count_if(v.begin(), v.end(), Greater20()); cout << "大于20的元素个数为:" << 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; }; class AgeGreater20 { public: bool operator()(Person &p) { return p.m_Age > 20; } }; void test02() { vector<Person>v; Person p1("刘备", 35); Person p2("关羽", 35); Person p3("张飞", 35); Person p4("赵云", 30); Person p5("曹操", 20); v.push_back(p1); v.push_back(p2); v.push_back(p3); v.push_back(p4); v.push_back(p5); //统计大于20岁的人员个数 int num = count_if(v.begin(), v.end(), AgeGreater20()); cout << "年龄大于20岁的人员个数为:" << num << endl; } int main() { test01(); test02(); system("pause"); return 0; }
random_shuffle(iterator beg, iterator end);
#include <algorithm>
#include <vector>
#include <ctime>
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
srand((unsigned int)time(NULL));
vector<int> v;
for(int i = 0 ; i < 10;i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), myPrint());
cout << endl;
//打乱顺序
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
#include <algorithm>
#include <vector>
class myPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10 ; i++)
{
v1.push_back(i);
v2.push_back(i + 1);
}
vector<int> vtarget;
//目标容器需要提前开辟空间
vtarget.resize(v1.size() + v2.size());
//合并 需要两个有序序列
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vtarget.begin());
for_each(vtarget.begin(), vtarget.end(), myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}