简体   繁体   English

boost :: iterator_range的rbegin()

[英]rbegin() of a boost::iterator_range

I refactored a class that used a single list of elements so it now uses a list of such lists. 我重构了一个使用单个元素列表的类,所以它现在使用这样的列表列表。 To minimize the changes in derived classes, I implemented a custom iterator using boost::iterator_facade and also a method to get a boost::iterator_range<iterator> that can be used for iterating instead of the original list. 为了最小化派生类的变化,我使用boost::iterator_facade实现了一个自定义iterator boost::iterator_facade并且还获得了一个方法来获取可以用于迭代而不是原始列表的boost::iterator_range<iterator>

This appears to work except in one place where rbegin() is used. 这似乎有效,除了在一个使用rbegin()地方。 boost::iterator_range does not appear to support something like that. boost::iterator_range似乎不支持类似的东西。

What would be a simple way of getting the range's last element? 获得范围最后一个元素的简单方法是什么?

I'm using VS2008 SP1, ie only some C++11 support in std::tr1, and boost is obviously available, too. 我正在使用VS2008 SP1,即std :: tr1中只支持一些C ++ 11,而且显然也可以使用boost。

typedef std::deque<MyData> DataList;

class MyClass : private boost::noncopyable
{
public:
    void AppendData(DataList* newData    

private:
    typedef std::deque<DataList*> ListOfDatatLists;

    /**
     * Custom iterator.
     * The content is not meant to be modified so this iterator operates on const lists only.
     */
    class iterator
        : public boost::iterator_facade <
        iterator,
        MyData,
        boost::forward_traversal_tag // Only forward iteration necessary
        >
    {
    public:
        static boost::iterator_range<iterator> range(const ListOfDataLists * pListOfLists);

    private:
        friend class boost::iterator_core_access;

        iterator(const ListOfDataLists * pListOfLists = NULL) : m_pListOfLists(pListOfLists) {}

        /// \name Implementations for boost base class
        //{@
        bool equal(iterator const & other) const;
        MyData & dereference() const;
        void increment();
        difference_type distance_to(const iterator & other) const;
        //@}

        const ListOfDataLists * m_pListOfLists;
        ListOfDataLists::const_iterator m_listIt; ///< The current list of data items
        DataList::const_iterator m_dataIt; ///< An iterator of the current list
    };


    ListOfResultLists m_dataLists;


protected:
    typedef std::tr1::shared_ptr<CLockedResults> SpLockedResults;

    /// For use by derived classes instead of the former single list
    boost::iterator_range<iterator> GetData() const;
};
  1. One solution, if possible, is to allow bidirectional or random access traversal. 如果可能,一种解决方案是允许双向或随机访问遍历。 This would allow you to get call range.end() -- (assuming the range isn't empty). 这将允许您获得调用range.end() -- (假设范围不为空)。 This would also allow you to reverse the range using the reversed boost range adaptor. 这也允许您使用reversed升压范围适配器来reversed范围。

  2. The other solution is to obtain the iterators from the range using begin()/end() and using std::distance to determine the distance between the two iterators. 另一种解决方案是使用begin()/end()并使用std::distance来确定两个迭代器之间的std::distance ,从范围中获取迭代器。 You could then use std::advance the beginning iterator one less than the distance to move to the last element. 然后你可以使用std::advance开始迭代器一个小于距离移动到最后一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM