Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 626915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:28:16+00:00 2026-05-13T19:28:16+00:00

I am building an app that needs to have support for two dimensional arrays

  • 0

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 vectors rather than arrays, and I was wondering what the best practices were for using 2d vectors. Should I have a vector of vectors of MapCells? or should it be a vector of vectors of pointers to MapCells? or references to MapCells?

class Map {
private:
    vector<vector<MapCell>> cells;

public:
    void loadMap() {
        cells.clear();
        for (int i = 0; i < WIDTH; i++) {
            //How should I be allocating these?
            vector<MapCell> row(HEIGHT);
            for (int j = 0; j < HEIGHT; j++) {
                //Should I be dynamically allocating these?
                MapCell cell;
                row.push_back(cell);
            }
            cells.push_back(row);
        }
    }
}

Basically what way of doing this is going to get me in the least amount of trouble (with respect to memory management or anything else)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:28:16+00:00Added an answer on May 13, 2026 at 7:28 pm

    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<MapCell> cells;
    
    public:
      void loadMap() {
        Matrix<MapCell> 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 <algorithm>
    #include <memory>
    #include <vector>
    
    template<class T, class A=std::allocator<T> >
    struct Matrix {
      typedef T value_type;
      typedef std::vector<value_type, A> 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<class Stream>
      friend Stream& operator<<(Stream& s, Matrix const& value) {
        s << "<Matrix at " << &value << " dimensions "
          << value.dim_a() << 'x' << value.dim_b();
        if (!value.empty()) {
          bool first = true;
          typedef typename Container::const_iterator Iter;
          Iter i = value._data.begin(), end = value._data.end();
          while (i != end) {
            s << (first ? " [[" : "], [");
            first = false;
            s << *i;
            ++i;
            for (int b = value._b - 1; b; --b) {
              s << ", " << *i;
              ++i;
            }
          }
          s << "]]";
        }
        s << '>';
        return s;
      }
    
    private:
      Container _data;
      int _b;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a app that needs to perform calculations on money. I wonder how
I'm building an app that pulls data in from an excel .csv file and
I am building an app that needs to dynamically/programatically know of and use different
I'm building a simple app too that needs to access a calendar that's in
I am building a silverlight application that needs to have a login page and
I'm building a app that need manage money datatype. I'm new on Obj-c, so
I'm building an app that authors would (hopefully) use to help them, uh.. author
We're building an app that stores hours of operation for various businesses. What is
I am building an app that talks to an Access database via OleDB/Jet. There
So I'm building an app that uses win32's SendMessage as IPC. I'm using FindWindow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.