C++ Two Dimensional std::vector best practices

后端 未结 4 982
栀梦
栀梦 2021-02-13 14:38

I am building an app that needs to have support for two dimensional arrays to hold a grid of data. I have a class Map that contains a 2d grid of data. I want to use

4条回答
  •  醉话见心
    2021-02-13 14:58

    When you want a square or 2d grid, do something similar to what the compiler does for multidimensional arrays (real ones, not an array of pointers to arrays) and store a single large array which you index correctly.

    Example using the Matrix class below:

    struct Map {
    private:
      Matrix cells;
    
    public:
      void loadMap() {
        Matrix cells (WIDTH, HEIGHT);
    
        for (int i = 0; i < WIDTH; i++) {
          for (int j = 0; j < HEIGHT; j++) {
            // modify cells[i][j]
          }
        }
    
        swap(this->cells, cells);
        // if there's any problem loading, don't modify this->cells
        // Matrix swap guarantees no-throw (because vector swap does)
        // which is a common and important aspect of swaps
      }
    };
    

    Variants of matrix classes abound, and there are many ways to tailor for specific use. Here's an example in less than 100 lines that gets you 80% or more of what you need:

    #include 
    #include 
    #include 
    
    template >
    struct Matrix {
      typedef T value_type;
      typedef std::vector Container;
    
      Matrix() : _b(0) {}
      Matrix(int a, int b, value_type const& initial=value_type())
      : _b(0)
      {
        resize(a, b, initial);
      }
      Matrix(Matrix const& other)
      : _data(other._data), _b(other._b)
      {}
    
      Matrix& operator=(Matrix copy) {
        swap(*this, copy);
        return *this;
      }
    
      bool empty() const { return _data.empty(); }
      void clear() { _data.clear(); _b = 0; }
    
      int dim_a() const { return _b ? _data.size() / _b : 0; }
      int dim_b() const { return _b; }
    
      value_type* operator[](int a) {
        return &_data[a * _b];
      }
      value_type const* operator[](int a) const {
        return &_data[a * _b];
      }
    
      void resize(int a, int b, value_type const& initial=value_type()) {
        if (a == 0) {
          b = 0;
        }
        _data.resize(a * b, initial);
        _b = b;
      }
    
      friend void swap(Matrix& a, Matrix& b) {
        using std::swap;
        swap(a._data, b._data);
        swap(a._b,    b._b);
      }
    
      template
      friend Stream& operator<<(Stream& s, Matrix const& value) {
        s << "';
        return s;
      }
    
    private:
      Container _data;
      int _b;
    };
    

提交回复
热议问题