无法使用 std::string 作为键迭代 std::map

发布于 2024-12-18 01:41:08 字数 1144 浏览 3 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-12-25 01:41:08

迭代器没有关系可比性,仅用于相等。所以说iter!=eiter

编写循环的噪音较小的方法:(

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

通常最好 typedef 映射类型!)

或者,在 C++11 中:

for (auto it = receive_times.cbegin(), end = receive_timed.cend(); it != end; ++it)

甚至:

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

Iterators are not relationally comparable, only for equality. So say iter != eiter.

A less noisy way to write the loop:

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

(Usually best to typedef the map type!)

Or, in C++11:

for (auto it = receive_times.cbegin(), end = receive_timed.cend(); it != end; ++it)

Or even:

for (const auto & p : receive_times)
{
  // do something with p.first and p.second
}
廻憶裏菂餘溫 2024-12-25 01:41:08

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

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

The idiomatic loop structure for container iterators is:

for (iter = receive_times.begin(); iter != eiter; ++iter)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文