Clean ways to write multiple 'for' loops

前端 未结 16 974
傲寒
傲寒 2020-12-22 15:22

For an array with multiple dimensions, we usually need to write a for loop for each of its dimensions. For example:

vector< vector< vector         


        
16条回答
  •  醉话见心
    2020-12-22 15:48

    The first thing is that you don't use such a data structure. If you need a three dimensional matrix, you define one:

    class Matrix3D
    {
        int x;
        int y;
        int z;
        std::vector myData;
    public:
        //  ...
        int& operator()( int i, int j, int k )
        {
            return myData[ ((i * y) + j) * z + k ];
        }
    };
    

    Or if you want to index using [][][], you need an operator[] which returns a proxy.

    Once you've done this, if you find that you constantly have to iterate as you've presented, you expose an iterator which will support it:

    class Matrix3D
    {
        //  as above...
        typedef std::vector::iterator iterator;
        iterator begin() { return myData.begin(); }
        iterator end()   { return myData.end();   }
    };
    

    Then you just write:

    for ( Matrix3D::iterator iter = m.begin(); iter != m.end(); ++ iter ) {
        //  ...
    }
    

    (or just:

    for ( auto& elem: m ) {
    }
    

    if you have C++11.)

    And if you need the three indexes during such iterations, it's possible to create an iterator which exposes them:

    class Matrix3D
    {
        //  ...
        class iterator : private std::vector::iterator
        {
            Matrix3D const* owner;
        public:
            iterator( Matrix3D const* owner,
                      std::vector::iterator iter )
                : std::vector::iterator( iter )
                , owner( owner )
            {
            }
            using std::vector::iterator::operator++;
            //  and so on for all of the iterator operations...
            int i() const
            {
                ((*this) -  owner->myData.begin()) / (owner->y * owner->z);
            }
            //  ...
        };
    };
    

提交回复
热议问题