std::map<int,int>与向量的向量

std::maplt;int, intgt; vs. vector of vector(std::maplt;int,intgt;与向量的向量)
本文介绍了std::map<int,int>与向量的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个容器来根据源 (int) 和目标 (int) 这两个属性存储值 (int),即当源向目标发送某些内容时,我需要将其作为元素存储在容器中.源由唯一的 int ID(从 0-M 的整数)标识,其中 M 在几十到几百之间,目标 (0-N) 也是如此.容器将通过另一个函数的迭代来更新.

我一直在使用 vector(vector(int)),这意味着按 source(destination(value)) 的顺序进行.后续过程需要检查此容器,以查看特定源和特定目的地的元素是否存在 - 它需要区分空的空间"和填充的空间".容器有可能非常稀疏.

要存储的值可以是 0,所以我没有成功尝试找出空间是否为空,因为我似乎无法执行 container[M][N].empty() 之类的操作.

我没有使用地图的经验,但我看过另一个 帖子 表明地图可能有用,并且 std::map<int, int> 似乎类似于 vector.

总结一下:

  1. 有没有办法检查向量空间"的特定向量是否为空(因为我无法将其与 0 进行比较)
  2. std::map<int, int> 是否更适合此目的,我该如何使用?

解决方案

我需要一个容器来根据两个属性存储一个值(int),源 (int) 和目标 (int)

std::map

<块引用>

后续进程需要检查这个容器,看看是否有元素存在于一个特定的来源和一个特定的目的地 - 它需要区分一个空的空间"和一个装满的.

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;与向量的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

What is the proper function for comparing two C-style strings?(比较两个 C 风格字符串的正确函数是什么?)
Image Capture with OpenCV - Select Timeout Error(使用 OpenCV 捕获图像 - 选择超时错误)
SHA256 HMAC using OpenSSL 1.1 not compiling(使用 OpenSSL 1.1 的 SHA256 HMAC 未编译)
How to make a Debian package depend on multiple versions of libboost(如何制作一个Debian包依赖于多个版本的libboost)
Why does strcpy_s not exist anywhere on my system?(为什么我系统上的任何地方都不存在 strcpy_s?)
Simplest way to get current time in current timezone using boost::date_time?(使用 boost::date_time 在当前时区获取当前时间的最简单方法?)