如何在三维数组中使用std::map来存储/检索具有特定值的键
How to use std::map in three dimensional array for storing/retrieving key with specific value
你能告诉我们如何使用std:map作为三维数组吗?我想像mymap[I][j][k]一样访问各个元素,以检查它是否存在,如果存在,它包含什么值,以及它是否与我的期望值匹配。如果它与我的期望值匹配,那么从映射中删除条目(键,值)。这就是我如何创建三维数组和迭代器-
std::map<int, map<int, map<int, unsigned long long int> > > MyMap;
std::map<int, map<int, map<int, unsigned long long int> > >::iterator it;
if (I==0 && j==4 && k==0) MyMap[i][j][k]= MemAddress.unlonglong(); //store a value to key
//Now I want to check if key exists where i=0,j=4,k=0 and if it is having "0x1234567" as expected value. If key value matches with my expected value then delete the key-value entry from MyMap and print whole table (MyMap) at the end.
有多种方法可以做到这一点。您可以使用自定义结构作为密钥,如@Krom建议的,如下所示:
struct Key { int a, b, c; };
(...)
std::map<Key, unsigned long long int> MyMap;
您也可以使用元组:
std::map<std::tuple<int, int, int>, unsigned long long int> MyMap;
或者成对使用(我认为你不应该这样做,因为它不干净)
std::map<std::pair<int, std::pair<int, int>>, unsigned long long int> MyMap;
仅仅使用结构可能是最干净的选择。使用该结构,您可以通过以下方式访问它(遵循示例代码):
if (I==0 && j==4 && k==0) MyMap[{i, j, k}]= MemAddress.unlonglong();
要插入新元素,您也可以使用:
MyMap.insert(std::make_pair<Key, unsigned long long int>({i, j, k}, MemAddress.unlonglong());
相关文章:
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- C++-需要读取存储在文件(.txt)中的时间戳并正确检索
- 我正在编写一个代码来将 int 数组存储在文件中,然后用 c++ 检索它,但是检索第一项是假值,我该如何解决这个问题?
- SQLite3 C++检索 ID 并存储到另一个表
- 如何在文件中节省空间地存储和检索 std::vector<int> 值
- 如何从 linux 二进制文件中存储和检索数据
- 如何将浮子值的字节存储在字符串中并以后检索值
- 如何在三维数组中使用std::map来存储/检索具有特定值的键
- 存储和检索位字段中的有符号值
- 如何通过先前存储的ID从结构对象中检索值?C
- 手动检索和存储指向 OpenGL 函数的指针时遇到问题
- c++使用字符串句柄对对象进行通用存储和检索
- 使用 XPTR 在内存中存储和检索矩阵
- 以数组的形式存储组合框项,并在 WPF 中检索 SelectedId
- 使用 QSqlQuery 检索 MySQL 存储过程的输出
- 使用c++通过文件存储和检索链表
- 从数据库中以 blob 数据类型形式存储的原始图像数据中检索 CImage 对象
- 从矢量中存储和检索IplImages
- 使用自动 c++ 检索存储元组的值
- 如何检索存储在列表中的一对点