Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ HASMAP;更新添加新密钥而不更新_C++_Hashmap - Fatal编程技术网

C++ HASMAP;更新添加新密钥而不更新

C++ HASMAP;更新添加新密钥而不更新,c++,hashmap,C++,Hashmap,我有一段代码,其中我获取一个hashmap,获取一个键并更新值。但是,循环永远不会结束,因为代码正在向hashmap添加新键,而不是更新。如果您能提供帮助,请告诉我:- typedef indri::utility::HashTable< Gram*, double, Gram::hash,Gram::string_comparator > HGramScore; HGramScore _gramScores; HGramScore::iterator iter_score;

我有一段代码,其中我获取一个hashmap,获取一个键并更新值。但是,循环永远不会结束,因为代码正在向hashmap添加新键,而不是更新。如果您能提供帮助,请告诉我:-

typedef indri::utility::HashTable< Gram*, double, Gram::hash,Gram::string_comparator > HGramScore;

 HGramScore _gramScores;

HGramScore::iterator iter_score;

cout << "Sizeee " << _gramScores.size() << endl;

int count = 0;  

for (iter_score = _gramScores.begin(); iter_score != _gramScores.end() ; iter_score++)

{

  count++;

  cout << "Sizeee " << _gramScores.size() << endl;

   _gramScores.insert(*iter_score->first , *iter_score->second / total_score_count);

  cout << count << endl;

}

看起来您每次都在通过循环添加一个项目,因此它永远不可能完成。你的意图是什么?很难知道你想做什么,但我想你应该简单地设置*iter\u score->second/=total\u score\u count;。若这仅仅是更新映射到总计数除以的值的所有分数,则不需要插入。您的描述获取一个键并更新该值表明这可能是正确的操作过程。@WhozCraig有效。但是,我想知道为什么在hashmap中插入一个具有现有键的值来更新该值不起作用?原因正是tadman提到的。不管怎样,对我真正的提示是说明更新的描述。他们显然支持的多映射场景中的哈希表条目不会使用insert调用进行更新。您可以使用返回的迭代器更新hashed-to值,并直接将hashed-to-target更改为目标,迭代器的第二个函数应该引用该值。