什么是使用两个键与std :: map的最好的方法? [英] What is the best way to use two keys with a std::map?

查看:134
本文介绍了什么是使用两个键与std :: map的最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个std :: map,我用来存储x& y坐标。我的数据非常稀疏,所以我不想使用数组或向量,这将导致大量的内存浪费。我的数据范围从-250000到250000,但我最多只有几千点。

I have a std::map that I'm using to store values for x & y coordinates. My data is very sparse, so I don't want to use arrays or vectors, which would result in a massive waste of memory. My data ranges from -250000 to 250000 but I'll only have a few thousand points at the most.

目前我创建一个带有两个坐标(即12x45)的std :: string并将其作为键。这似乎不是最好的方法。

Currently I'm creating a std::string with the two coordinates (ie "12x45") and using it as a key. This doesn't seem like the best way to do it.

我的其他想法是使用一个int64,并把两个int32s,并使用它作为一个键。

My other thoughts were to use an int64 and shove the two int32s into it and use it as a key.

或使用具有两个坐标的类。对用作键的类有什么要求?

Or to use a class with the two coordinates. What are the requirements on a class that is to be used as the key?

这是什么最好的方法?

What is the best way to do this? I'd rather not use a map of maps.

推荐答案

使用std :: pair< int32,int32>

Use std::pair<int32,int32> for the key:

std::map<std::pair<int,int>, int> myMap;

myMap[std::make_pair(10,20)] = 25;
std::cout << myMap[std::make_pair(10,20)] << std::endl;

这篇关于什么是使用两个键与std :: map的最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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