如何在地图中存储结构?
问题描述:
是否可以在地图中存储结构?
以及,如果是的话,如何?
例如:
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);