如何仅使用单个向量从头开始实现Array2D?

问题描述 投票:0回答:1

[不会说谎,这是家庭作业。但是几天来我一直在努力寻找解决方案,但我做不到。基本上,我们的想法是我们必须仅使用单个向量来实现2D数组。这是老师提供的头文件:

#ifndef _ARRAY2D
#define _ARRAY2D

#include <vector>

namespace math
{

//------------------------------------ class Array2D ------------------------------------------------

    /*! It is the class that represents a generic data container for a two-dimensional array of data.
     * 
     * It holds the actual buffer of the data values and provides methods for accessing them, 
     * either as individual tokens or as a memory block. 
     *
     */ 
    template <typename T>
    class Array2D
    {
    protected:
        std::vector<T> buffer;                       

        unsigned int width,                          
                     height;                         

    public:
        /*! Returns the width of the array
         */
        const unsigned int getWidth() const;

        /*! Returns the height of the array
         */
        const unsigned int getHeight() const;

        // data accessors and mutators

        /*! Obtains a pointer to the internal data.
         *
         *  This is NOT a copy of the internal array data, but rather a pointer 
         *  to the internally allocated space, so DO NOT attempt to delete the pointer. 
         */
        T * getRawDataPtr();

        /*! Copies the array data from an external raw buffer to the internal array buffer. 
         *
         *  The member function ASSUMES that the input buffer is of a size compatible with the internal storage of the 
         *  Array2D object. If the array buffer cannot be properly resized or the width or height of the array are 0, 
         *  the method should exit immediately.
         *
         *  \param data_ptr is the reference to the preallocated buffer from where to copy the data to the Array2D object.
         */
        void setData(const T * const & data_ptr);            

        /*! Returns a reference to the stored item at location (x,y).
         *
         *  \param x is the horizontal coordinate of the item.
         *  \param y is the vertical coordinate of the item.
         *
         *  \return a reference to the item at location (x,y).
         */
        T & operator () (unsigned int x, unsigned int y);

        // constructors and destructor

        /*! Constructor with data initialization.
         * 
         * Default parameters let it also be used as a default constructor.
         *
         * \param width is the desired width of the new array.
         * \param height is the desired height of the new array.
         * \param data_ptr is the source of the data to copy to the internal array buffer. 
         * If none is provided, but the width and height are non-zero, the buffer is initialized to default values (all zero - black).
         * 
         * \see setData
         */ 
        Array2D(unsigned int width = 0, unsigned int height = 0, const T * data_ptr = 0);

        /*! Copy constructor.
         *
         * \param src is the source array to replicate in this object.
         */
        Array2D(const Array2D &src);

        /*! The Array2D destructor.
         */
        ~Array2D();

        /*! Copy assignment operator.
         *
         * \param right is the source array.
         */
        Array2D & operator = (const Array2D & right);

    };

}   //namespace math

/* Do not modify the current file to provide the implementation for the 2D array class. Instead, use the array2d.hpp file to
   provide the difinitions of the class member functions:
*/
#include "array2d.hpp"


#endif

这是我到目前为止在代码中所做的事情,我认为它根本不应该工作:

#include "array2d.h"
namespace math {
    template<typename T>
    const unsigned int Array2D<T>::getWidth() const {
        return this->width;
    }
    template<typename T>
    const unsigned int Array2D<T>::getHeight() const {
        return this->height;
    }
    template<typename T>
    T* Array2D<T>::getRawDataPtr() {
        return this->buffer;
    }
    template<typename T>
    void Array2D<T>::setData(const T* const& data_ptr) {
        if (getWidth() == 0 || getHeight() == 0 || getRawDataPtr == 0)
            return;
        for (unsigned int i = 0; i < getWidth() * getHeight(); i++)
            this->buffer[i] = data_ptr[i];
    }
    template<typename T>
    T& Array2D<T>::operator()(unsigned int x, unsigned int y){
        return this->buffer[x + y * height];
    }
    template<typename T>
    Array2D<T>::Array2D(unsigned int width, unsigned int height, const T * data_ptr) :
    width(width)
    height(height)
    buffer(data_ptr)
    {}
    template<typename T> 
    Array2D<T>::Array2D(const Array2D& src) {
        std::vector<T> vec(src.getWidth() * src.getHeight());
        this->width = src.getWidth();
        this->height = src.getHeight();
        this->buffer = src.buffer;
    }

    template<typename T>
    Array2D<T>::~Array2D() {

    }
    template<typename T>
    Array2D<T>& Array2D<T>::operator=(const Array2D& right) {

    }
}

我只需要有关功能的帮助。

c++ arrays visual-c++ c++17
1个回答
0
投票

但是您的陈述“我认为一点都不奏效”是含糊的,在您的代码中有许多明显的问题。首先,在需要原始指针的地方使用向量:

template<typename T>
    T* Array2D<T>::getRawDataPtr() {
        return this->buffer;
    }

您是说this->buffer.data()吗?

接下来,您在构造函数中执行的分配不正确:

    template<typename T>
    Array2D<T>::Array2D(unsigned int width, unsigned int height, const T * data_ptr) :
    width(width)
    height(height)
    buffer(data_ptr)
    {}

无法从原始指针初始化向量。

不说空的operator=和未使用的std::vector<T> vec(src.getWidth() * src.getHeight());

© www.soinside.com 2019 - 2024. All rights reserved.