c++ - unordered_multimap.empty() 返回真,即使我认为它应该返回假?
问题描述
我是使用哈希表的新手(AFAIK unordered_multimap 是一个哈希表),我正在尝试使用函数在其中插入一个结构。我的代码:
点头
#pragma once
#include <iostream>
#include <unordered_map>
struct nod {
int stare[100], pathManhattan, depth;
nod* nodParinte;
char actiune;
nod();
void Citire(int n);
void Init(std::unordered_multimap<int, nod*> hashTable);
};
Nod.cpp
#include "Nod.h"
#include <unordered_map>
nod::nod()
{
pathManhattan = 0;
depth = 0;
nodParinte = NULL;
}
void nod::Citire(int n)
{
for (int i = 0; i < n*n; i++)
{
std::cin >> stare[i];
}
}
void nod::Init(std::unordered_multimap<int, nod*> hashTable)
{
hashTable.insert({ pathManhattan + depth, this });
hashTable.empty() ? std::cout << "da" : std::cout << "nu";
}
控制台应用程序.cpp
#include <iostream>
#include <string>
#include <unordered_map>
#include "Nod.h"
std::unordered_multimap<int, nod*> theExplored;
std::unordered_multimap<int, nod*> theFrontier;
int main()
{
nod nodInitial; int n, t[1000];
nodInitial.Init(theExplored);
theExplored.empty() ? std::cout << "da" : std::cout << "nu";
return 0;
}
来自 Init 的这一行
hashTable.empty() ? std::cout << "da" : std::cout << "nu";
返回假
而来自 Consoleapplication.cpp 的这一行返回 true
theExplored.empty() ? std::cout << "da" : std::cout << "nu";
我不明白为什么。
有人可以向我解释一下吗?
我想在结构中创建一个函数,它将 nod 类型的特定变量插入到已探索的哈希表中,以 int 作为键,但我不知道该怎么做 - 我认为这可能是合适的方法,但似乎不是。
解决方案
void nod::Init(std::unordered_multimap<int, nod*> hashTable)
这需要它的参数值。这意味着它会制作一个副本并且只修改这个本地副本。原始地图保持不变。通过引用传递:
void nod::Init(std::unordered_multimap<int, nod*> &hashTable)
// ~^~
推荐阅读
- linux - 将文件的内容通过管道传输到 AWK
- vb.net - 可以获取 PDF 字体编码值吗?
- python - python - 如何在python中为每列提供标题名称时进行数据清理并将结果导出到excel?
- vuejs3 - Vue 3:无法读取 null 的属性“id”
- reactjs - 任何正确/干净的方法来处理反应表中的大量 if..then 案例?
- javascript - 电子:打印 iframe 给出参考
- javascript - VBA 网页点击
- javascript - express.js:将上传的图像传递给 s3
- kubernetes - Istio 安装因私有 docker 注册表而失败
- c - 使用 printf、pipe 和 fork 时的奇怪行为