C++ 无法使用std::字符串作为键在std::map上迭代

C++ 无法使用std::字符串作为键在std::map上迭代,c++,iterator,stdstring,stdmap,C++,Iterator,Stdstring,Stdmap,我的问题几乎与相同,但那里的解决方案并没有解决我的错误 在main.h中,我有: #include <map> #include <string> std::map<std::string, int64_t> receive_times; 但是,当我尝试编译时,会出现以下错误: error: invalid operands to binary expression ('std::map<std::string, int64_t>::const

我的问题几乎与相同,但那里的解决方案并没有解决我的错误

main.h
中,我有:

#include <map>
#include <string>

std::map<std::string, int64_t> receive_times;
但是,当我尝试编译时,会出现以下错误:

error: invalid operands to binary expression ('std::map<std::string, int64_t>::const_iterator' (aka '_Rb_tree_const_iterator<value_type>') and 'std::map<std::string, int64_t>::const_iterator'
  (aka '_Rb_tree_const_iterator<value_type>'))
  for (iter = receive_times.begin(); iter < eiter; ++iter)
                                     ~~~~ ^ ~~~~~
错误:二进制表达式('std::map::const_iterator'(又称“'urb_tree_const_iterator”)和'std::map::const_iterator'的操作数无效
(又名“\u Rb\u tree\u const\u iterator”))
for(iter=receive_times.begin();iter

我在顶部链接到的问题中的解决方案是因为缺少了一个
#include
,但显然我已经包含了它。有什么提示吗?

迭代器在关系上是不可比较的,只是为了相等。所以说
iter!=eiter

编写循环的噪音更小的方法:

for (std::map<std::string, int64_t>::const_iterator iter = receive_times.begin(),
     end = receive_times.end(); iter != end; ++iter)
{
  // ...
}
甚至:

for (const auto & p : receive_times)
{
  // do something with p.first and p.second
}

容器迭代器的惯用循环结构是:

for (iter = receive_times.begin(); iter != eiter; ++iter)

您不应该在头文件中定义变量。。。
for (const auto & p : receive_times)
{
  // do something with p.first and p.second
}
for (iter = receive_times.begin(); iter != eiter; ++iter)