0

単一の要素リストを使用していたクラスをリファクタリングしたので、そのようなリストのリストを使用するようになりました。派生クラスの変更を最小限に抑えるために、元のリストの代わりに反復に使用できるa を取得するカスタムiteratorusingとメソッドを実装しました。boost::iterator_facadeboost::iterator_range<iterator>

これは、 が使用されている 1 か所を除いて機能するようrbegin()です。boost::iterator_rangeそのようなものをサポートしていないようです。

範囲の最後の要素を取得する簡単な方法は何でしょうか?

私は 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;
};
4

1 に答える 1

2
  1. 1 つの解決策は、可能であれば、双方向またはランダム アクセス トラバーサルを許可することです。これにより、呼び出しを受けることができますrange.end() --(範囲が空でない場合)。reversedこれにより、ブースト範囲アダプターを使用して範囲を逆にすることもできます。

  2. もう 1 つの解決策は、2 つのイテレータ間の距離を決定するためにusingbegin()/end()および using を使用して範囲からイテレータを取得することです。次に、最後の要素に移動する距離よりも 1 小さい開始イテレータstd::distanceを使用できます。std::advance

于 2012-06-22T11:59:05.270 に答える