在地图中搜索 [英] Searching in a map

查看:76
本文介绍了在地图中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个带有struct指针的地图作为键。


std :: map< struct1 *,vector< struct2>>


struct1 {


int id;


int type;


字节名称[32]; 


};



struct2 {


int id;


int type;


字节名称[32]; 


int version;


int code;


};


键是指向struct1的指针。 struct1有一个名为id的成员。


我必须通过struct1的成员id搜索地图。


是否可以在没有循环的情况下进行搜索通过地图中的所有元素?

解决方案

我从未尝试过,但您可以指定用于对地图进行排序的比较对象。  ;如果构造对象来比较成员id的值而不是结构的地址,它可以做你想要的。


没有这个,我认为你的问题的答案是"否]。


Hi,

I have a map with pointer of struct as key.

std::map<struct1*, vector<struct2>>

struct1{

int id;

int type;

byte name[32]; 

};

struct2{

int id;

int type;

byte name[32]; 

int version;

int code;

};

The key is the pointer to struct1. the struct1 has the member called id.

I have to search the map by the member id of struct1.

Is it possible to search without looping through all the elements in the map?

解决方案

I have never tried but you can specify the comparison object used to sort the map.  If you construct the object to compare the value of member id instead of the address of the struct it may do what you want.

Without this, I think the answer to your question is "no."


这篇关于在地图中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆