我想提供一个对2个容器的内容进行迭代的迭代器。

例如,我想隐藏以下事实:折线的节点存储在两个容器中(出于实现目的):

struct PolyLine {
private:
    vector<Point*> m_head_nodes;
    vector<Point*> m_tail_nodes;

public:
    Iterator begin();
    Iterator end();
};

Polyline poly;
cout << "contents of poly:" << endl;
for(Point *p : poly)
   cout << p << endl;

迭代器应先迭代m_head_nodes,然后再迭代m_tail_nodes。

问题1:您能否演示如何设置Iterator对象?
  • 从第一个容器到第二个容器时,如何实现operator ++?

  • Q2:如果第二个容器是std::list,该怎么办?
  • 您使用什么构造来表示当前的“位置”迭代器?
  • 您如何表示end()


  • 我已经尝试过以下实现方式,

    struct Iterator
    {
        PolyLine &m_parent;
        vector<Point*>::iterator m_it;
    
        Iterator(PolyLine &parent_container)
            : m_parent(parent_container) {
        }
    
        Iterator& operator++() {
            if (m_it == m_parent.m_head_nodes.end())
                m_it = m_parent.m_tail_nodes.begin();
            else
                ++m_it;
            return *this;
        }
    
        Point * operator* () {
             return *m_it;
        }
    };
    
    bool operator== (Iterator &one, Iterator &other) {
        return one.m_it == other.m_it;
    }
    
    Iterator Polyline::begin() {
        Iterator o(this);
        o.m_it = m_head_nodes.begin();
        return o;
    }
    
    Iterator Polyline::end() {
        Iterator o(this);
        o.m_it = m_tail_nodes.end();
        return o;
    }
    

    但我不希望保留指向PolyLine类的指针。

    另外,如果第二个容器是m_it,我不知道保留什么作为std::list

    最佳答案

    这样做是否对您有用(显然,这在10分钟的解决方案中是失败的,因此不要指望委员会在c++ 20或类似的东西中安装它-只是给出一些想法):

    #include <iostream>
    #include <array>
    #include <vector>
    #include <deque>
    #include <algorithm>
    
    
    template<typename Pointee0, typename Pointee1, typename It0, typename It1> struct ChainedIter;
    
    template<typename Pointee, typename It0, typename It1>
    class ChainedIter<Pointee, Pointee, It0, It1> {
        It0 it0, begin0, end0;
        It1 it1, begin1, end1;
    public:
        ChainedIter(It0 begin0, It0 end0, It1 begin1, It1 end1):
            it0{begin0}, begin0{begin0}, end0{end0},
            it1{begin1}, begin1{begin1}, end1{end1} {}
        bool operator==(ChainedIter& rhs) const {
            return it0 == rhs.it0 && it1 == rhs.it1;
        }
        bool operator!=(ChainedIter& rhs) const {
            return !(*this == rhs);
        }
        ChainedIter* operator++() {
            if(it0 != end0) ++it0;
            else ++it1;
            return this;
        }
        Pointee& operator*() {
            if(it0 != end0) return *it0;
            else return *it1; // UB if it1 == end1
        }
        ChainedIter end() {
            auto newChainedIter = *this;
            newChainedIter.it0 = newChainedIter.end0;
            newChainedIter.it1 = newChainedIter.end1;
            return newChainedIter;
    
        }
        ChainedIter begin() {
            auto newChainedIter = *this;
            newChainedIter.it0 = newChainedIter.begin0;
            newChainedIter.it1 = newChainedIter.begin1;
            return newChainedIter;
    
        }
    };
    
    template<typename Cont1, typename Cont0>
    decltype(auto) createIter(Cont0& cont0, Cont1& cont1) {
        auto begin0 = cont0.begin();
        auto end0 = cont0.end();
        auto begin1 = cont1.begin();
        auto end1 = cont1.end();
        return ChainedIter<
               typename Cont0::value_type,
               typename Cont1::value_type,
               typename Cont0::iterator,
               typename Cont1::iterator> {begin0, end0, begin1, end1};
    }
    
    int main() {
        std::vector<size_t> v(4, 20);
        std::deque<size_t> d(3, 200);
    
        auto iter = createIter(v, d);
        std::for_each(iter.begin(), iter.end(), [](const auto& elt) {
            std::cout << elt << ' ';
        });
        std::cout << std::endl;
    }
    

    我试图使其与不同的容器类型一起使用,只要它们都在同一对象上进行模板化(这很容易理解,但也许可以增强以允许可转换类型等)。从main()中可以看出,它可以与vectordeque一起使用。

    我的编译器版本是:
    $ g++ --version
    g++ (GCC) 9.1.0
    Copyright (C) 2019 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    

    因此,也许我可以使用C++ 17的模板指南来不依​​赖于该附加函数来简化类型推断,但这本身使我键入了10多分钟,然后花了更多的时间来整理出编译错误。再加上我敢肯定,无论如何,这里还有很多其他可怕的产品:P

    09-25 18:01
    查看更多