std :: map< int,int>与矢量矢量 [英] std::map<int, int> vs. vector of vector

查看:156
本文介绍了std :: map< int,int>与矢量矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个容器来根据两个属性source(int)和destination(int)存储一个值(int),即当一个源发送到目的地时,我需要将它作为元素存储在容器中。源由唯一的int ID(0-M的整数)标识,其中M是几十到几百,目的地(0-N)也是如此。



我一直在使用向量(vector(int))这意味着按照源(目的地(值))的顺序。后续进程需要检查此容器,以查看特定源和特定目标是否存在元素 - 它将需要区分空空格和已填充的空格。容器有非常稀疏的可能性。



要存储的值可以是0,所以我没有成功试图找出空间是否为空,因为我似乎不能做像container [M] [N] .empty()。



我没有地图使用经验,但我看到了另一个 post 表明一个映射可能是有用的,并且 std :: map 类似于向量<向量< int>>



总结:


  1. 有一种方法可以检查向量'space'的特定向量是否为空(因为我无法将其与0比较)

  2. std :: map 更好的用途,我如何使用?


解决方案


我需要一个容器根据两个属性存储一个值b source(int)and destination(int)




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




后续进程需要检查此容器, b $ b元素存在于特定源中,特定的
目标 - 它将需要区分空的'空间'
和填充的空间。


< blockquote>

  std :: map :: find 

http://www.cplusplus.com/reference/ map / map / find /


容器可能非常稀疏。


使用std :: map。容器的正确选择是基于你需要找到的东西,以及你需要插入/删除的东西。如果你想快速找到东西,使用地图。


I need a container to store a value (int) according to two attributes, source (int) and destination (int) i.e. when a source sends something to a destination, I need to store it as an element in a container. The source is identified by a unique int ID (an integer from 0-M), where M is in the tens to hundreds, and so is the destination (0-N). The container will be updated by iterations of another function.

I have been using a vector(vector(int)) which means goes in the order of source(destination(value)). A subsequent process needs to check this container, to see if an element exists in for a particular source, and a particular destination - it will need to differentiate between an empty 'space' and a filled one. The container has the possibility of being very sparse.

The value to be stored CAN be 0 so I haven't had success trying to find out if the space is empty, since I can't seem to do something like container[M][N].empty().

I have no experience with maps, but I have seen another post that suggests a map might be useful, and an std::map<int, int> seems to be similar to a vector<vector<int>>.

To summarise:

  1. Is there a way to check if a specific vector of vector 'space' is empty (since I can't compare it to 0)
  2. Is a std::map<int, int> better for this purpose, and how do I use one?

解决方案

I need a container to store a value (int) according to two attributes, source (int) and destination (int)

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

A subsequent process needs to check this container, to see if an element exists in for a particular source, and a particular destination - it will need to differentiate between an empty 'space' and a filled one.

std::map::find

http://www.cplusplus.com/reference/map/map/find/

The container has the possibility of being very sparse.

Use a std::map. The "correct" choice of a container is based on how you need to find things and how you need to insert/delete things. If you want to find things fast, use a map.

这篇关于std :: map&lt; int,int&gt;与矢量矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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