地图插入返回错误答案的地图
Map of map insert returning incorrect answer
我正在使用地图中的地图来跟踪一些数据。
为了添加一些数据,我首先检查我是否基于字符串形式的全局键进行跟踪。
如果我的地图中有该键,那么我会检查第一个地图内的地图,看看它是否有值,其中键作为无符号的短整型。这是结构。
map<string, fileInfo> fileMap;
class fileInfo {
public:
unsigned short int numNeighbors;
map<unsigned short int, neighborInfo> neighbors; //key is id
//... more
}
我能够检查我的主键是否在我的整个地图中,但是当我检查该键是否在那里时,我在插入时得到一个 false,表明这个邻居已经存在,但它不存在。我已经检查了理智,并确保查看我的密钥是否匹配,但它们不匹配。
查找第一个键
it = fileMap.find(filename);
if (it == fileMap.end()) {
//add to fileMap
}else{
fileInfo currFI = it->second;
neighborInfo n = createNeighbor();
pair<it_Neighbor, bool> ret;
map<unsigned short int, neighborInfo> neighbors = currFI.neighbors;
ret = currFI.neighbors.insert(pair<unsigned short int, neighborInfo> (id, n));
if (ret.second == false) {
cout<< "Neighbor already exists for this file, ignoring duplicates."<< endl;
} else {
cout << "size" << currFI.neighbors.size() << endl;
}
我的大小输出始终为 2,而它应该是 4。
我是否需要在我的类中定义任何运算符或任何内容才能使其工作?
而不是
fileInfo currFI = it->second;
尝试
fileInfo &currFI = it->second;
当 else 块结束时,currFI
变量将被丢弃。您应该将其设置为fileMap
项的reference
,以便将其保留在那里。
相关文章:
- 欧拉项目#8答案是大以获得有效答案
- 为什么不;名字在地图上是按顺序排列的吗
- 基于多个条件处理地图中的所有元素
- 首要问题的答案让值班员搞错了
- 在C++中将矢量转换为嵌套地图
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 替换基于地图的所有引用
- 如何区分地图中的 0 和 false?
- 地图计数确实很重要,或者只是检查是否存在
- 如何从地图中删除矢量对象
- 使用 bfs 解决连接组件问题时得到错误的答案
- 是否有任何C++功能可以对地图进行排序?
- 如何使用 std::variant 打印地图键/值?
- 从矢量或地图中删除共享指针
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- 同一对象的"sizeof"的不同答案
- 无限嵌套具有变体的地图
- C++一会儿循环读到地图上 2 行?
- 地图插入返回错误答案的地图