如何在地图中存储结构? [英] How to store a structure in a map?

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

问题描述

是否可以在地图中存储结构?
以及,如果是的话,如何?
例如:

Is it possible to store a structure in a map?
and, if yes, how?
e.g.:

struct struct1
{
int a;
int b;
float c;
fload d;
};



映射的键应为c,并且映射的值应包含整个结构.



key of the map should be c and the mapped value should contain entire structure.

推荐答案

是的,可以尝试以下代码
Yes, it is possible, try this code
typedef struct struct1{
    int a;
    int b;
    float c;
    float d;
} my_struct_t;
//...
my_struct_t st = { 1, 2, 3.0, 4,0 };
std::map< float, my_struct_t > my_struct_map;
std::pair< float, my_struct_t > p = std::make_pair( st.c, st );
my_struct_map.insert(p);


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

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