常量映射迭代器不会设置为 mymap.begin()

发布于 2024-11-04 16:38:08 字数 327 浏览 1 评论 0原文

map<string,Shopable*>::iterator it = mymap.begin();

迭代器看起来是常量,但 items.begin() 不返回常量迭代器。或者,这就是我的想法,因为鼠标悬停错误类似于:

"No conversion from 'std::Tree_const_iterator<...> to std::Tree_iterator<...> exists'".

为什么?

map<string,Shopable*>::iterator it = mymap.begin();

The iterator appears to be constant, but items.begin() doesn't return a constant iterator. Or, that's what I think because the mouseover error is something like:

"No conversion from 'std::Tree_const_iterator<...> to std::Tree_iterator<...> exists'".

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

牵强ㄟ 2024-11-11 16:38:08

使用 const_iterator as :

map<string,Shopable*>::const_iterator it = mymap.begin();

从错误中可以清楚地看出,mymap.begin() 返回 const_iterator。这是因为 mymap 在您编写此代码的函数中是 const,如下所示:

void f(const std::map<int,int> & m)
{    //^^^^^ note this

      std::map<int,int>::const_iterator it = m.begin(); //m is const in f()
                       //^^^^^ note this
}

void g(std::map<int,int> & m)
{
      std::map<int,int>::iterator it = m.begin(); //m is non-const in g()
}

const 容器(无论其 < code>std::map、std::vector 等)返回 const_iterator,非常量容器返回 iterator

每个容器都有重载函数 begin()end()。因此,const 容器调用重载的 begin(),它返回 const_iterator,非常量容器调用另一个重载的 begin() code> 返回迭代器。对于 end() 重载函数也是如此。

Use const_iterator as :

map<string,Shopable*>::const_iterator it = mymap.begin();

From the error, its clear that mymap.begin() returns const_iterator. That is because mymap is const in the function where you've written this, something like following:

void f(const std::map<int,int> & m)
{    //^^^^^ note this

      std::map<int,int>::const_iterator it = m.begin(); //m is const in f()
                       //^^^^^ note this
}

void g(std::map<int,int> & m)
{
      std::map<int,int>::iterator it = m.begin(); //m is non-const in g()
}

That is, const container (whether its std::map, std::vector etc) returns const_iterator and non-const container returns iterator.

Every container has overloaded functions of begin() and end(). So const container invokes the overloaded begin() which returns const_iterator and non-const container invokes the other overloaded begin() which returns iterator. And same for end() overloaded functions.

轻许诺言 2024-11-11 16:38:08

问题是上面代码中的 mymap 是常量映射,而不是可变映射(也许它是类的成员,并且该代码位于常量成员函数内部?)。因此,对 mymap.begin() 的调用将获取返回 const_iterator 的重载,而不是返回 iterator 的重载。

如果不需要通过迭代器更改容器,请使用const_iterator。如果您打算修改映射,请确保在循环中使用非常量对象(也许成员函数(如果是这种情况)不应该是 const?)

The problem is that mymap in the code above is a constant map, not a mutable map (maybe it is a member of a class and that code is inside constant member function?). Thus the call to mymap.begin() will pichup the overload that returns a const_iterator instead of the overload that returns an iterator.

If you do not need to change the container through the iterator, use const_iterator. If you intend on modifying the map, make sure that you are using a non-const object for the loop (maybe the member function (if that is the case) should not be const?)

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