算法简介:
find
//查找元素find_if
//按条件查找元素adjacent_find
//查找相邻重复元素binary_search
//二分查找法count
//统计元素个数count_if
//按条件统计元素个数功能描述:
函数原型:
下面演示了查找内置数据类型和自定义数据类型。
#include
#include
#include
#include
using namespace std;
//查找内置数据类型
void test1()
{
vector<int>v;
for (int i = 0;i < 10;i++)
{
v.push_back(i);
}
//查找是否有5
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到:" << *it << endl;
}
}
//查找自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
bool operator==(const Person&p)
{
if (this->name == p.name && this->age == p.age)
{
return true;
}
else
{
return false;
}
}
string name;
int age;
};
void test2()
{
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);
Person pp("bbb", 20);
vector<Person>::iterator it = find(v.begin(), v.end(), pp);
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到:姓名:" << it->name << " 年龄:" << it->age << endl;
}
}
int main()
{
test1();
test2();
}
功能描述:
函数原型:
find_if(iterator beg, iterator end, _Pred)
#include
#include
#include
#include
using namespace std;
class GreatFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test1()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreatFive());
if (it == v.end())
{
cout << "未找到" << endl;
}
else
{
cout << "找到了:" << *it << endl;
}
}
//自定义数据类型
class Person
{
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
string name;
int age;
};
class Greater20
{
public:
bool operator()(const Person & p)
{
return p.age > 20;
}
};
void test2()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("aaa", 20);
Person p3("aaa", 30);
Person p4("aaa", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
if (it == v.end())
{
cout << "未找到" << endl;
}
else
{
cout << "找到了:" << it->age << endl;
}
}
int main()
{
test1();
test2();
}
功能描述:
函数原型:
adjacent_find(iterator beg, iterator end);
#include
#include
#include
using namespace std;
void test1()
{
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()
{
test1();
}
功能描述:
函数原型:
bool binary_search(iterator beg, iterator end, value);
#include
#include
#include
using namespace std;
void test1()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//v.push_back(2);
//容器必须是有序序列
bool ret = binary_search(v.begin(), v.end(), 9);
if (ret)
{
cout << "找到了" << endl;
}
else
{
cout << "未找到" << endl;
}
}
int main()
{
test1();
}
功能描述:
函数原型:
count(iterator beg, iterator end, value);
#include
#include
#include
using namespace std;
void test1()
{
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);
int num = count(v.begin(), v.end(), 40);
cout << "40的元素个数为:" << num << endl;
}
class Person
{
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
bool operator==(const Person &p)
{
return this->age == p.age;
}
string name;
int age;
};
void test2()
{
vector<Person>v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("张飞", 35);
Person p4("赵云", 40);
Person p5("曹操", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
Person p("诸葛亮", 35);
int num = count(v.begin(), v.end(), p);
cout << "和诸葛亮同岁数的个数为:" << num << endl;
}
int main()
{
test1();
}
功能描述:
函数原型:
count_if(iterator beg, iterator end, _Pred);
#include
#include
#include
using namespace std;
class Greater20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
void test1()
{
vector<int>v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(20);
int num = count_if(v.begin(), v.end(), Greater20());
cout << "大于20的元素个数为:" << num << endl;
}
class Person
{
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
string name;
int age;
};
class AgeGreater20
{
public:
bool operator()(const Person& p)
{
return p.age > 20;
}
};
void test2()
{
vector<Person>v;
Person p1("刘备", 35);
Person p2("关羽", 35);
Person p3("张飞", 35);
Person p4("赵云", 40);
Person p5("曹操", 20);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num = count_if(v.begin(), v.end(), AgeGreater20());
cout << "大于20岁的人员个数:" << num << endl;
}
int main()
{
test1();
test2();
}