获取 set 的元素

        最近在用 map 和 set ,两个一起使用,set 作为 map 的 value。突然发现要获取 set 里的元素有点曲折啊,记录一下,方便以后随时查看。 我们知道 set 和 map 底层实现都是二叉树,自定义类型必须重载一个小于号运算符。

#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <set>

enum label_type
{
	LABEL_TYPE_ONE,
	LABEL_TYPE_TWO, 
	LABEL_TYPE_THREE
};

struct label_map
{
	int label;
	enum label_type type;
};

bool operator<(const struct label_map& lhs, const struct label_map& rhs)
{
	return lhs.label < rhs.label;
}

std::map<int, std::set<struct label_map>> map_set;

void getSetOfMap(int key, std::set<struct label_map> &label)
{
	auto it = map_set.find(key);
	if(it != map_set.end())
	{
		label = map_set[key];
	}
}

void getSetElement(int key, struct label_map *label)
{
	std::set<struct label_map> st;
	getSetOfMap(key, st);
	auto it = st.begin();
	*label = *it;
}

int main()
{
	struct label_map label_1 = {1001, LABEL_TYPE_ONE};
	struct
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值