#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
void find1()
{
vector<int >v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector <int >::iterator pos = find(v.begin(), v.end(), 5);
if (pos!=v.end())
{
cout << "找到了。。。" << endl;
}
else
{
cout << "未找到。。。" << endl;
}
}
class Person {
public:
Person(string name,int age)
{
m_age = age;
m_name = name;
}
bool operator==(const Person &p)const
{
if (this->m_name == p.m_name&&this->m_age == p.m_age)
return true;
return false;
}
string m_name;
int m_age;
};
void find2()
{
vector<Person>v;
Person p1("a", 11);
Person p2("b", 22);
Person p3("c", 33);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
vector <Person>::iterator pos = find(v.begin(), v.end(), p2);
if (pos != v.end())
{
cout << "找到了:" << (*pos).m_name << (*pos).m_age << endl;
}
else
{
cout << "未找到。。。" << endl;
}
}
class MyCompare :public binary_function<Person*, Person*, bool > {
public:
bool operator()(const Person * p1,const Person * p2) const
{
if (p1->m_name == p2->m_name && p1->m_age == p2->m_age)
{
return true;
}
return false;
}
};
void find3()
{
vector<Person*>v;
Person p1("a", 11);
Person p2("b", 22);
Person p3("c", 33);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
Person *p = new Person("b", 22);
vector <Person*>::iterator pos = find_if(v.begin(), v.end(), bind2nd(MyCompare(), p));
if (pos != v.end())
{
cout << "找到了:" << (*pos)->m_name << (*pos)->m_age << endl;
}
else
{
cout << "未找到。。。" << endl;
}
}
void find4()
{
vector<int>v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos != v.end())
{
cout << "找到了相邻元素" << endl;
}
else
{
cout << "未找到相邻。。" << endl;
}
}
void find5()
{
vector<int >v;
for (int i=0;i<10;i++)
{
v.push_back(i);
}
bool ret=binary_search(v.begin(), v.end(), 4);
if (ret)
{
cout << "找到find5"<<endl;
}
else
{
cout << "未找到find5"<<endl;
}
}
void find6()
{
vector<int >v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
int num=count(v.begin(),v.end(),4);
cout <<"出现次数:"<< num<<endl;
}
class ThenFour {
public:
bool operator()(int v)
{
return v > 4;
}
};
void find7()
{
vector<int >v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
int num = count_if(v.begin(), v.end(), ThenFour());
cout << "出现大于4的次数:" << num << endl;
}
int main()
{
find1();
find2();
find3();
find4();
find5();
find6();
find7();
}