当容器定义为 const 时,带有自定义迭代器的自定义容器不起作用

发布于 2024-10-12 14:25:58 字数 283 浏览 1 评论 0原文


或多或少一切都在主题中。
当我有

func(my_cont& c)
{ c.begin() };

东西工作,但

func(const my_cont& c)
{ c.begin() };

不起作用时,编译器声称无法将其从 my_cont<..> 转换为 const my_cont<..>....

容器和我的自定义迭代器处理此问题有哪些要求?

More or less everything is in the topic.
when I have

func(my_cont& c)
{ c.begin() };

things works, but

func(const my_cont& c)
{ c.begin() };

doesn't work, compiler claims that cant convert this to const my_cont<..>.... from my_cont<..>

What are the requirements for the container and my custom iterator to handle this?

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

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

发布评论

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

评论(5

风为裳 2024-10-19 14:25:58

您的容器类应该有两个 begin() 和 end() 实现 - 一个返回迭代器,另一个返回 const_iterator:

      iterator begin ();
const_iterator begin () const;
      iterator end ();
const_iterator end () const;

const_iterator 不能用于修改它指向的对象。

Your container class should have two implementations of begin() and end() - one returning iterator and another returning const_iterator:

      iterator begin ();
const_iterator begin () const;
      iterator end ();
const_iterator end () const;

const_iterator cannot be used to modify the object that it points to.

猛虎独行 2024-10-19 14:25:58

您需要添加一个 const begin(),如下所示:

class my_cont
{
public:
    const_iterator begin() const;
};

您还必须定义一个 const_iterator 类型。

You need to add a const begin(), something like this:

class my_cont
{
public:
    const_iterator begin() const;
};

You'll have to define a const_iterator type also.

孤独难免 2024-10-19 14:25:58

这是一篇关于编写迭代器的好文章:
http://www.aristeia.com/Papers/CUJ_June_2001.pdf

大多数人都有注意到您需要一个返回 const_iterator 的 begin() 和 end() 的 const 版本。

大多数人忘记的是迭代器应该能够隐式转换为 const_iterator。否则,很难从非成本对象中获得 const 迭代器(没有大量令人讨厌的强制转换)。

 my_cont   data;
 for(my_cont::const_iterator loop = data.begin(); loop != data.end(); ++loop)
 {
     /* STUFF */
 }

注意:上面的调用实际上会调用非成本版本的 begin() 和 end()。但它们被分配给 const_iterator。因此,您的迭代器必须可转换为 const_iterator 才能使上述代码正常工作(注意:没有从 const_iterator 到迭代器的隐式转换。这应该采用显式 const_cast,因为它本质上是危险的)。

class my_cont
{
    public:
    class iterator { /* STUFF */ }
    class const_iterator
    {
        public: const_iterator(iterator const& rhs);
        /* STUFF */
    }

    iterator       begin();
    iterator       end();

    const_iterator begin() const;
    const_iterator end()   const;
    /* STUFF*/ 
};

Here is a good article on writing iterators:
http://www.aristeia.com/Papers/CUJ_June_2001.pdf

As most people have noted you need a const version of begin() and end() that returns a const_iterator.

What most people have forgotten is that an iterator should be able to implicitly convert into a const_iterator. Otherwise it is hard to get a const iterator from a non cost object (without a lot of nasty casts).

 my_cont   data;
 for(my_cont::const_iterator loop = data.begin(); loop != data.end(); ++loop)
 {
     /* STUFF */
 }

Note above: The above calls will actually call the non cost versions begin() and end(). But they are being assigned to a const_iterator. So your iterators must be convertible to const_iterator for the above code to work (Note: There is no implicit conversion from const_iterator to iterator. That should take an explicit const_cast as it is inherently dangerous).

class my_cont
{
    public:
    class iterator { /* STUFF */ }
    class const_iterator
    {
        public: const_iterator(iterator const& rhs);
        /* STUFF */
    }

    iterator       begin();
    iterator       end();

    const_iterator begin() const;
    const_iterator end()   const;
    /* STUFF*/ 
};
梦里梦着梦中梦 2024-10-19 14:25:58

这正是STL为容器实现cost_iterator的原因。只是检查它在STL中是如何实现的,我不确定你能找到更好的解决方案。

This is exactly the reason why STL implements cost_iterator for containers. Just check how is it implemented in STL, I'm not sure you can find any better solution.

心欲静而疯不止 2024-10-19 14:25:58

您只能在 const 对象上调用 const 成员函数(或在 易失性对象上调用易失性成员函数)。
编译器告诉我们缺少iterator my_cont::begin() const

You can only call const member functions on a const object (or volatile member functions on a volatile object).
The compiler is telling that iterator my_cont::begin() const is missing.

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