函数原型:
代码语言:javascript复制#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
class person {
public:
string name;
int age;
person(string n,int a):name(n),age(a){}
//重载==运算符,返回值为bool类型
bool operator==( person &p1)
{
if (p1.name == name && p1.age == age)
{
return true;
}
return false;
}
};
void test01()
{
//相邻重复元素查找
vector<person> v;
person p1("孙悟空", 180);
person p2("沙僧", 60);
person p3("沙僧", 60);
person p4("白骨精", 18);
person p5("牛魔王", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
vector<person>::iterator it;
//如果要进行自定义数据类型对比,要重载==运算符
it=adjacent_find(v.begin(), v.end());
if (it == v.end())
{
cout << "没有相邻重复元素" << endl;
}
else
{
cout << "姓名: " << (*it).name << " 年龄: " << (*it).age << endl;
it ;
cout << "姓名: " << (*it).name << " 年龄: " << (*it).age << endl;
it ;
cout << "姓名: " << (*it).name << " 年龄: " << (*it).age << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}