• <tfoot id='RCNwK'></tfoot>

  • <legend id='RCNwK'><style id='RCNwK'><dir id='RCNwK'><q id='RCNwK'></q></dir></style></legend>
    • <bdo id='RCNwK'></bdo><ul id='RCNwK'></ul>

    <small id='RCNwK'></small><noframes id='RCNwK'>

    <i id='RCNwK'><tr id='RCNwK'><dt id='RCNwK'><q id='RCNwK'><span id='RCNwK'><b id='RCNwK'><form id='RCNwK'><ins id='RCNwK'></ins><ul id='RCNwK'></ul><sub id='RCNwK'></sub></form><legend id='RCNwK'></legend><bdo id='RCNwK'><pre id='RCNwK'><center id='RCNwK'></center></pre></bdo></b><th id='RCNwK'></th></span></q></dt></tr></i><div id='RCNwK'><tfoot id='RCNwK'></tfoot><dl id='RCNwK'><fieldset id='RCNwK'></fieldset></dl></div>

      1. C++ 中的自定义迭代器

        时间:2024-05-12
      2. <i id='M8HNr'><tr id='M8HNr'><dt id='M8HNr'><q id='M8HNr'><span id='M8HNr'><b id='M8HNr'><form id='M8HNr'><ins id='M8HNr'></ins><ul id='M8HNr'></ul><sub id='M8HNr'></sub></form><legend id='M8HNr'></legend><bdo id='M8HNr'><pre id='M8HNr'><center id='M8HNr'></center></pre></bdo></b><th id='M8HNr'></th></span></q></dt></tr></i><div id='M8HNr'><tfoot id='M8HNr'></tfoot><dl id='M8HNr'><fieldset id='M8HNr'></fieldset></dl></div>

          <legend id='M8HNr'><style id='M8HNr'><dir id='M8HNr'><q id='M8HNr'></q></dir></style></legend>

            <small id='M8HNr'></small><noframes id='M8HNr'>

              • <tfoot id='M8HNr'></tfoot>
                  <tbody id='M8HNr'></tbody>

                  <bdo id='M8HNr'></bdo><ul id='M8HNr'></ul>
                • 本文介绍了C++ 中的自定义迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 TContainer 类,它是几个指向 TItems 类的 stl 集合指针的集合.

                  I have a class TContainer that is an aggregate of several stl collections pointers to TItems class.

                  我需要创建一个迭代器来遍历 TContainer 类中所有集合中的元素,抽象出内部工作的客户端.

                  I need to create an Iterator to traverse the elements in all the collections in my TContainer class abstracting the client of the inner workings.

                  这样做的好方法是什么?我应该创建一个扩展迭代器的类(如果是,我应该扩展哪个迭代器类),我应该创建一个迭代器聚合的迭代器类吗?

                  What would be a good way to do this?. Should I crate a class that extends an iterator (if so, what iterator class should I extend), should I create an iterator class that is an aggregate of iterators?

                  我只需要一个 FORWARD_ONLY 迭代器.

                  I only need a FORWARD_ONLY iterator.

                  即,如果这是我的容器:

                  I.E, If this is my container:

                  typedef std::vector <TItem*> ItemVector;
                  class TContainer {
                     std::vector <ItemVector *> m_Items;
                  };
                  

                  什么是遍历 m_Items 成员变量的向量中包含的所有项目的好的迭代器.

                  What would be a good Iterator to traverse all the items contained in the vectors of the m_Items member variable.

                  推荐答案

                  当我做自己的迭代器时(不久前),我继承自 std::iterator 并将类型指定为第一个模板参数.希望有所帮助.

                  When I did my own iterator (a while ago now) I inherited from std::iterator and specified the type as the first template parameter. Hope that helps.

                  对于前向迭代器,在以下代码中使用 forward_iterator_tag 而不是 input_iterator_tag.

                  For forward iterators user forward_iterator_tag rather than input_iterator_tag in the following code.

                  这个类最初取自 istream_iterator 类(并为我自己使用而修改,因此它可能不再类似于 istram_iterator).

                  This class was originally taken from istream_iterator class (and modified for my own use so it may not resemble the istram_iterator any more).

                  template<typename T>
                  class <PLOP>_iterator
                           :public std::iterator<std::input_iterator_tag,       // type of iterator
                                                 T,ptrdiff_t,const T*,const T&> // Info about iterator
                  {
                      public:
                          const T& operator*() const;
                          const T* operator->() const;
                          <PLOP>__iterator& operator++();
                          <PLOP>__iterator operator++(int);
                          bool equal(<PLOP>__iterator const& rhs) const;
                  };
                  
                  template<typename T>
                  inline bool operator==(<PLOP>__iterator<T> const& lhs,<PLOP>__iterator<T> const& rhs)
                  {
                      return lhs.equal(rhs);
                  }
                  

                  查看有关迭代器标签的文档:
                  http://www.sgi.com/tech/stl/iterator_tags.html

                  Check this documentation on iterator tags:
                  http://www.sgi.com/tech/stl/iterator_tags.html

                  刚刚重新阅读了有关迭代器的信息:
                  http://www.sgi.com/tech/stl/iterator_traits.html

                  Having just re-read the information on iterators:
                  http://www.sgi.com/tech/stl/iterator_traits.html

                  这是旧的做事方式 (iterator_tags),更现代的方法是为您的迭代器设置 iterator_traits<> 以使其与 STL 完全兼容.

                  This is the old way of doing things (iterator_tags) the more modern approach is to set up iterator_traits<> for your iterator to make it fully compatible with the STL.

                  这篇关于C++ 中的自定义迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何从 STL 容器中擦除元素? 下一篇:C++ std::vector 是如何实现的?

                  相关文章

                  <small id='TMONX'></small><noframes id='TMONX'>

                  • <bdo id='TMONX'></bdo><ul id='TMONX'></ul>

                    <i id='TMONX'><tr id='TMONX'><dt id='TMONX'><q id='TMONX'><span id='TMONX'><b id='TMONX'><form id='TMONX'><ins id='TMONX'></ins><ul id='TMONX'></ul><sub id='TMONX'></sub></form><legend id='TMONX'></legend><bdo id='TMONX'><pre id='TMONX'><center id='TMONX'></center></pre></bdo></b><th id='TMONX'></th></span></q></dt></tr></i><div id='TMONX'><tfoot id='TMONX'></tfoot><dl id='TMONX'><fieldset id='TMONX'></fieldset></dl></div>
                    <legend id='TMONX'><style id='TMONX'><dir id='TMONX'><q id='TMONX'></q></dir></style></legend>
                    <tfoot id='TMONX'></tfoot>