无法使用 std::string 作为键迭代 std::map
我的问题几乎与这个问题相同,但是那里的解决方案没有解决我的错误。
在 main.h
中,我有:
#include <map>
#include <string>
std::map<std::string, int64_t> receive_times;
在 main.cpp
中:
std::map<std::string, int64_t>::const_iterator iter;
std::map<std::string, int64_t>::const_iterator eiter = receive_times.end();
for (iter = receive_times.begin(); iter < eiter; ++iter)
printf("%s: %ld\n", iter->first.c_str(), iter->second);
但是,当我尝试编译时,出现以下错误:
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)
~~~~ ^ ~~~~~
我链接到的问题中的解决方案top 是因为缺少 #include
,但显然我已经包含了它。有什么提示吗?
My question is almost identical to this one, but the solution there hasn't resolved my error.
In main.h
I have:
#include <map>
#include <string>
std::map<std::string, int64_t> receive_times;
And in main.cpp
:
std::map<std::string, int64_t>::const_iterator iter;
std::map<std::string, int64_t>::const_iterator eiter = receive_times.end();
for (iter = receive_times.begin(); iter < eiter; ++iter)
printf("%s: %ld\n", iter->first.c_str(), iter->second);
However, when I try and compile I get the following error:
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)
~~~~ ^ ~~~~~
The solution in the question I linked to at the top is because there was a missing #include <string>
, but obviously I have that included. Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
迭代器没有关系可比性,仅用于相等。所以说
iter!=eiter
。编写循环的噪音较小的方法:(
通常最好
typedef
映射类型!)或者,在 C++11 中:
甚至:
Iterators are not relationally comparable, only for equality. So say
iter != eiter
.A less noisy way to write the loop:
(Usually best to
typedef
the map type!)Or, in C++11:
Or even:
容器迭代器的惯用循环结构是:
The idiomatic loop structure for container iterators is: