0.前言

1.find


#include
using namespace std;
#include
#include
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
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->m_Name = name;
this->m_Age = age;
}
bool operator==(const Person&p)
{
if (p.m_Name == this->m_Name && p.m_Age == this->m_Age)
{
return true;
}
else
{
return false;
}
}
string m_Name;
int m_Age;
};
void test02()
{
Person p1("a", 10);
Person p2("b", 20);
Person p3("c", 30);
vector<Person>v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
Person p5("b", 20);
vector<Person>::iterator it = find(v.begin(), v.end(), p5);
if (it == v.end())
{
cout << "未找到" << endl;
}
else
{
cout << "找到: 姓名: " << it->m_Name << " age:" << (*it).m_Age << endl;
}
}
int main()
{
test01();
cout << "------------------------" << endl;
test02();
system("pause");
return 0;
}

2.find_if

#include
using namespace std;
#include
class Greater5
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), Greater5());
if (it == v.end())
{
cout << "no find" << endl;
}
else
{
cout << "find element: " << (*it) << endl;
}
}
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
bool greater_Age20(const Person& p)
{
return p.m_Age > 20;
}
void test02()
{
Person p1("a", 10);
Person p2("b", 20);
Person p3("c", 30);
vector<Person>v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
vector<Person>::iterator it = find_if(v.begin(), v.end(), greater_Age20);
if (it == v.end())
{
cout << "no find" << endl;
}
else
{
cout << "find the element: name:" << it->m_Name << " age:" << it->m_Age << endl;
}
}
int main()
{
test01();
cout << "------------------------" << endl;
test02();
system("pause");
return 0;
}

3.adjacent_find


#include
using namespace std;
#include
#include
void test01()
{
vector<int>v;
v.push_back(1);
v.push_back(2);
v.push_back(1);
v.push_back(3);
v.push_back(4);
v.push_back(3);
v.push_back(3);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos == v.end())
{
cout << "no find adjacent duplcate elements " << endl;
}
else
{
cout << "find adjacent duplcate elements: " << *pos << endl;
}
}
int main()
{
test01();
cout << "------------------------" << endl;
system("pause");
return 0;
}

4.binary_search


#include
using namespace std;
#include
#include
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
bool ret = binary_search(v.begin(), v.end(),9);
if (ret)
{
cout << "find point element" << endl;
}
else
{
cout << "no find" << endl;
}
}
int main()
{
test01();
cout << "------------------------" << endl;
system("pause");
return 0;
}

5.count


#include
using namespace std;
#include
#include
void test01()
{
vector<int>v;
v.push_back(1);
v.push_back(4);
v.push_back(1);
v.push_back(3);
int num = count(v.begin(), v.end(), 1);
cout << "the nmber of point element: " << num << endl;
}
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
bool operator==(const Person& p)
{
if (p.m_Age == this->m_Age)
{
return true;
}
else
{
return false;
}
}
string m_Name;
int m_Age;
};
void test02()
{
Person p1("刘备", 35);
Person p2("薇恩", 24);
Person p3("皮城", 35);
Person p4("光辉", 40);
vector<Person>v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
Person p5("卡米尔", 35);
int num = count(v.begin(), v.end(), p5);
cout << "跟卡米尔同岁的人有多少个: " << num << endl;
}
int main()
{
test01();
cout << "------------------------" << endl;
system("pause");
return 0;
}

6.count_if

#include
using namespace std;
#include
#include
class Greater3
{
public:
bool operator()(int val)
{
return val > 3;
}
};
void test01()
{
vector<int>v;
v.push_back(1);
v.push_back(6);
v.push_back(3);
v.push_back(2);
v.push_back(4);
v.push_back(3);
int num = count_if(v.begin(), v.end(), Greater3());
cout << "the number of element greater than 3 : " << num << endl;
}
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
bool age_Greater35(const Person& p)
{
if (p.m_Age > 35)
{
return true;
}
else
{
return false;
}
}
void test02()
{
Person p1("刘备", 35);
Person p2("薇恩", 24);
Person p3("皮城", 35);
Person p4("光辉", 40);
vector<Person>v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
int num = count_if(v.begin(), v.end(), age_Greater35);
cout << "the number of character whose age greater than 35 :" << num << endl;
}
int main()
{
test01();
cout << "------------------------" << endl;
test02();
system("pause");
return 0;
}
