#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
bool operator==(const Person& p)
{
return (this->m_name == p.m_name && this->m_age == p.age);
}
string m_name;
int m_age;
};
void print(vector<int>& v)
{
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int>v1;
for(int i = 0; i < 10; i++)
{
v1.push_back(i);
}
print(v1);
vector<int>iterator it = find(v1.begin(), v1.end(), 5);
vector<int>iterator it2 = find(v1.begin(), v1.end(), 50);
if(it == v1.end())
{
cout << "没有找到" << endl;
}
else{
cout << "找到:" << *it << endl;
}
if(it2 == v1.end())
{
cout << "没有找到" << endl;
}
else{
cout << "找到:" << *it << endl;
}
vector<Person>v2;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
v1.push_back(p4);
Person pp("bbb", 20);
vector<int>iterator it3 = find(v1.begin(), v1.end(), pp);
if(it3 == v2.end())
{
cout << "没有找到" << endl;
}
else{
cout << "找到:" << it->m_name << it->m_age << endl;
}
return 0;
}
/*output:
0 1 2 3 4 5 6 7 8 9
找到:5
没有找到
找到:bbb20
*/