stl 中list 或者vector正确使用find查找类对象

本文介绍了一个简单的C++示例,展示了如何为类对象重载等号运算符(==),并利用该重载实现查找功能。通过这个例子,读者可以了解基本的类成员函数定义和标准模板库(STL)中find函数的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键做好类对象的运算符重载

 

#include "stdafx.h"
#include <vector>
#include <algorithm>

class people
{
public:
 int age;
 char name[32];
public:
 bool operator == (const people&T)
 {
  if (age == T.age)
  {
   return true;
  }
  return false;
 }
};

typedef std::vector<people> myPeople;
typedef myPeople::iterator myPeopleIt;

int _tmain(int argc, _TCHAR* argv[])
{
 using namespace std;

 people m_p1;
 myPeople m_p;
 m_p.push_back(m_p1);
 myPeopleIt it = find(m_p.begin(),m_p.end(),m_p1);
 if (it != m_p.end())
 {
  printf("find it");
 }
 
 return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值