一、概述
- find //查找元素
- find_if //按条件查找元素
- adjacent_find //查找相邻重复元素
- binary_search //二分查找
- count //统计元素个数
- count_if //按条件统计元素个数
二、find ( iterator beg, iterator end, value)
// 按值查找元素,找到即返回指定位置迭代器,找不到返回end()
// beg开始迭代器, end结束迭代器, value是待查找元素
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
class temp
{
public:
temp(int num, string str)
{
this->t_num = num;
this->t_str = str;
}
int t_num;
string t_str;
//重载==运算符。此处必须参数列表必须加上const
bool operator==(const temp &p)
{
if(p.t_num == this->t_num && p.t_str == this->t_str)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
//查找内置数据类型,比如int
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
vector<int>::iterator it = find(v.begin(), v.end(), 30);
if(it == v.end())
{
cout << "Not found" << endl;
}
else
{
cout << "Has found: " << *it << endl;
}
//查找自定义数据类型,需要在写类的时候重载 == 运算符
vector<temp> v2;
temp t1(100, "abc");
temp t2(200, "def");
temp t3(300, "ghi");
v2.push_back(t1);
v2.push_back(t2);
v2.push_back(t3);
vector<temp>::iterator it2 = find(v2.begin(), v2.end(), t3);
if(it2 == v2.end())
{
cout << "Not found" << endl;
}
else
{
cout << "Has found" << endl;
}
system("pause");
}
运行结果:
Has found: 30
Has found
请按任意键继续. . .
三、find_if (iterator beg, iterator end, _Pred)
//按值查找函数,找到就返回指定位置迭代器,找不到返回end( )
//beg开始迭代器,end结束迭代器,_Pred函数或者谓词(返回bool类型的仿函数)
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
bool temp(int value)
{
return value > 5;
}
int main()
{
vector<int> v;
for (int i = 1; i <= 10; i++)
{
v.push_back(i);
}
//查找一个大于5的数
vector<int>::iterator it = find_if(v.begin(), v.end(), temp);
if(it==v.end())
{
cout << "Not found" << endl;
}
else
{
cout << "A number greater than 5 is : " << *it << endl;
}
system("pause");
}
运行结果:
A number greater than 5 is : 6
请按任意键继续. . .
四、adjacent_find (iterator beg, iterator end)
// 查找相邻重复元素,返回相邻元素第一个位置的迭代器
// beg开始迭代器
// end结束迭代器
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(3);
vector<int>::iterator it = adjacent_find(v.begin(), v.end());
if(it == v.end())
{
cout << "No duplicate numbers" << endl;
}
else
{
cout << "The repeated number is: " << *it << endl;
}
system("pause");
}
运行结果:
The repeated number is: 3
请按任意键继续. . .
五、bool binary_search (iterator beg, iterator end, value)
//查找指定的元素,若查找到返回true,否则返回false
//注意:二分查找只能在有序序列中可用,不可用于无序序列。而且必须是升序。
//beg是开始迭代器,end是结束迭代器,value是查找的元素
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
class temp
{
public:
bool operator()(int num1, int num2)
{
return num1 > num2;
}
};
void printSet(int num)
{
cout << num << ' ';
}
int main()
{
//默认升序排列
set<int> s;
for (int i = 1; i <= 10; i++)
{
s.insert(i);
}
for_each(s.begin(), s.end(), printSet);
cout << endl;
bool ret = binary_search(s.begin(), s.end(), 9);
if(ret == true)
{
cout << "9 is fonud" << endl;
}
else
{
cout << "9 is not found" << endl;
}
cout << endl;
//降序排列
set<int, temp> s2;
for (int i = 1; i <= 10; i++)
{
s2.insert(i);
}
for_each(s2.begin(), s2.end(), printSet);
cout << endl;
bool ret2 = binary_search(s2.begin(), s2.end(), 9);
if(ret2 == true)
{
cout << "9 is fonud" << endl;
}
else
{
cout << "9 is not found" << endl;
}
system("pause");
}
运行结果:
1 2 3 4 5 6 7 8 9 10
9 is fonud
10 9 8 7 6 5 4 3 2 1
9 is not found
请按任意键继续. . .
六、int count (iterator beg, iterator end, value)
// 用于统计元素出现的次数
// beg是开始迭代器,end是结束迭代器,value是待统计的元素
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
class person
{
public:
person(string str, int num)
{
this->name = str;
this->years = num;
}
bool operator==(const person &p)
{
if(p.years == this->years)
return true;
else
{
return false;
}
}
int years;
string name;
};
int main()
{
//默认数据类型,比如int
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(20);
v.push_back(30);
int num1 = count(v.begin(), v.end(), 20);
cout << num1 << endl;
//自定义数据类型,需要在类中重载==运算符
vector<person> v2;
person p1("Frank", 18);
person p2("Joe", 19);
person p3("Andy", 19);
person p4("Patrick", 19);
v2.push_back(p1);
v2.push_back(p2);
v2.push_back(p3);
int num2 = count(v2.begin(), v2.end(), p4);
cout << "The number of person who has the same years old as Patrick's is " << num2 << endl;
system("pause");
}
运行结果:
2
The number of person who has the same years old as Patrick's is 2
请按任意键继续. . .
七、int count_if (iterator beg, iterator end, _Pred)
// 按条件统计元素出现次数
// beg开始迭代器,end结束迭代器,_Pred函数或者谓词(返回bool类型的仿函数)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void printVector(const int num)
{
cout << num << ' ';
}
bool temp(const int num)
{
return num >= 5;
}
int main()
{
vector<int> v;
for (int i = 1; i <= 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), printVector);
cout << endl;
int num = count_if(v.begin(), v.end(), temp);
cout << "There has " << num << " numbers not smaller than 5." << endl;
system("pause");
}
运行结果:
1 2 3 4 5 6 7 8 9 10
There has 6 numbers not smaller than 5.
请按任意键继续. . .