C++查找Vector中结构体中的数据中的内容find()

自定义的结构体

typedef struct CollectionLevel_s
{
	int collectionPos;
	int level;
}CollectionLevel_t;

typedef struct CollectionLevelCount_s
{
	CollectionLevel_t CollectionLevel;
	int count;

	bool operator == (const CollectionLevel_t& e) {
		return (this->CollectionLevel.collectionPos == e.collectionPos) && (this->CollectionLevel.level == e.level);
	}

	bool operator == (const int& pos) {
		return (this->CollectionLevel.collectionPos == pos);
	}

}CollectionLevelCount_t;

使用函数查找

使用函数查找的时候,必须重载操作符才可以。

查找容器中的CollectionLevelCount_s中的CollectionLevel数据是否相等。

需要重载操作符

bool operator == (const CollectionLevel_t& e) {
		return (this->CollectionLevel.collectionPos == e.collectionPos) && (this->CollectionLevel.level == e.level);
	}

CollectionLevelCount_t data;
data.count=0;
data.CollectionLevel.collectionPos=10;
data.CollectionLevel.level=1;
std::vector<CollectionLevelCount_t>ve_collectionLevelCount;
auto tmp = find(ve_collectionLevelCount.begin(), ve_collectionLevelCount.end(),data);
if (tmp == ve_collectionLevelCount.end())
{
//没有找到数据
}
else
{
//找到了数据
}

查找容器中的CollectionLevelCount_s中的CollectionLevel数据中的collectionPos数据是否相等。

需要重载操作符

	bool operator == (const int& pos) {
		return (this->CollectionLevel.collectionPos == pos);
	}

例子:

//自己准备数据
auto tmp = find(ve_collectionLevelCount.begin(), ve_collectionLevelCount.end(), elem1.collectionPos);
 if (tmp == ve_collectionLevelCount.end())
{
//查找到对应的collectionPos对应相等的数据了
}
else
{
//没有找到数据
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值