无序的_多重映射-迭代find()的结果会产生具有不同值的元素 < C++中的MultMAP看起来很奇怪,我想知道为什么 #include <iostream> #include <unordered_map> using namespace std; typedef unordered_multimap<char,int> MyMap; int main(int argc, char **argv) { MyMap map; map.insert(MyMap::value_type('a', 1)); map.insert(MyMap::value_type('b', 2)); map.insert(MyMap::value_type('c', 3)); map.insert(MyMap::value_type('d', 4)); map.insert(MyMap::value_type('a', 7)); map.insert(MyMap::value_type('b', 18)); for(auto it = map.begin(); it != map.end(); it++) { cout << it->first << '\t'; cout << it->second << endl; } cout << "all values to a" << endl; for(auto it = map.find('a'); it != map.end(); it++) { cout << it->first << '\t' << it->second << endl; } }

无序的_多重映射-迭代find()的结果会产生具有不同值的元素 < C++中的MultMAP看起来很奇怪,我想知道为什么 #include <iostream> #include <unordered_map> using namespace std; typedef unordered_multimap<char,int> MyMap; int main(int argc, char **argv) { MyMap map; map.insert(MyMap::value_type('a', 1)); map.insert(MyMap::value_type('b', 2)); map.insert(MyMap::value_type('c', 3)); map.insert(MyMap::value_type('d', 4)); map.insert(MyMap::value_type('a', 7)); map.insert(MyMap::value_type('b', 18)); for(auto it = map.begin(); it != map.end(); it++) { cout << it->first << '\t'; cout << it->second << endl; } cout << "all values to a" << endl; for(auto it = map.find('a'); it != map.end(); it++) { cout << it->first << '\t' << it->second << endl; } },c++,c++11,stl,iteration,unordered-map,C++,C++11,Stl,Iteration,Unordered Map,当我显式地请求“a”时,为什么输出仍然包含以b为键的内容?这是编译器还是stl错误?find,在实现时,返回与多重映射中的键匹配的第一个元素的迭代器(与任何其他映射一样)。您可能正在寻找: //查找包含键为k的所有元素的范围。 //配对相等范围(常数键类型和k) 自动its=map.equal_范围('a'); for(auto it=its.first;it!=its.second;++it){ cout first似乎您在对的完整“列表”中得到了一个迭代器,从第一对开始,以“a”作为键。因此

当我显式地请求“a”时,为什么输出仍然包含以b为键的内容?这是编译器还是stl错误?

find
,在实现时,返回与多重映射中的键匹配的第一个元素的迭代器(与任何其他映射一样)。您可能正在寻找:

//查找包含键为k的所有元素的范围。
//配对相等范围(常数键类型和k)
自动its=map.equal_范围('a');
for(auto it=its.first;it!=its.second;++it){

cout first似乎您在对的完整“列表”中得到了一个迭代器,从第一对开始,以“a”作为键。因此,当您迭代到最后,自然您也会得到“a”之外的所有内容。如果您搜索“c”,您可能会迭代整个“列表”做你在那里做的事情。也许你应该迭代到“it!=map.end()&&it->first=='a'”,如果你想要所有的a。

这不是一个bug,它是设计的。
find
返回一个迭代器到匹配的元素之一,仅此而已。你将用你的构造迭代到映射的末尾

您需要使用来完成所需的操作。

中有一个示例,关于如何使用等范围方法来获取具有相同键的所有元素

// unordered_multimap::equal_range
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>

typedef std::unordered_multimap<std::string,std::string> stringmap;

int main ()
{
  stringmap myumm = {
     {"orange","FL"},
     {"strawberry","LA"},
     {"strawberry","OK"},
     {"pumpkin","NH"}
  };

  std::cout << "Entries with strawberry:";
  auto range = myumm.equal_range("strawberry");
  for_each (
    range.first,
    range.second,
    [](stringmap::value_type& x){std::cout << " " << x.second;}
  );

  return 0;
}
//无序的多重映射::相等的范围
#包括
#包括
#包括
#include

@einpoklum删除前这是一个答案请阅读此meta让我重新表述一下(审查机制不允许您发表具体评论,您可以从列表中进行选择):OP问“为什么会发生X?”-您的答案虽然可能在总体上很有用,但并不是对OP代码发生了什么的解释。因此,这不是一个答案。
// Finds a range containing all elements whose key is k.
// pair<iterator, iterator> equal_range(const key_type& k)
auto its = map.equal_range('a');
for (auto it = its.first; it != its.second; ++it) {
    cout << it->first << '\t' << it->second << endl;
}
// unordered_multimap::equal_range
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>

typedef std::unordered_multimap<std::string,std::string> stringmap;

int main ()
{
  stringmap myumm = {
     {"orange","FL"},
     {"strawberry","LA"},
     {"strawberry","OK"},
     {"pumpkin","NH"}
  };

  std::cout << "Entries with strawberry:";
  auto range = myumm.equal_range("strawberry");
  for_each (
    range.first,
    range.second,
    [](stringmap::value_type& x){std::cout << " " << x.second;}
  );

  return 0;
}