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

  • Home
  • SEARCH
  • 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 8782829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:40:12+00:00 2026-06-13T20:40:12+00:00

What is the correct way to implement an efficient 2d vector? I need to

  • 0

What is the correct way to implement an efficient 2d vector? I need to store a set of Item objects in a 2d collection, that is fast to iterate (most important) and also fast to find elements.

I have a 2d vector of pointers declared as follows:

std::vector<std::vector<Item*>> * items;

In the constructor, I instantiate it as follows:

items = new std::vector<std::vector<Item*>>();
items->resize(10, std::vector<Item*>(10, new Item()));

I how do I (correctly) implement methods for accessing items? Eg:

items[3][4] = new Item();

AddItem(Item *& item, int x, int y)
{
     items[x][y] = item;
}

My reasoning for using pointers is for better performance, so that I can pass things around by reference.

If there is a better way to go about this, please explain, however I would still be interested in how to correctly use the vector.

Edit: For clarification, this is part of a class that is for inventory management in a simple game. The set 10×10 vector represents the inventory grid which is a set size. The Item class contains the item type, a pointer to an image in the resource manager, stack size etc.

My pointer usage was in an attempt to improve performance, since this class is iterated and used to render the whole inventory every frame, using the image pointer.

  • 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-06-13T20:40:13+00:00Added an answer on June 13, 2026 at 8:40 pm

    It seems that you know the size of the matrix beforehand, and that this matrix is squared. Though vector<> is fine, you can also use native vectors in that case.

    Item **m = new Item*[ n * n ];
    

    If you want to access position r,c, then you only have to multiply r by n, and then add c:

    pos = ( r * n ) + c;
    

    So, if you want to access position 1, 2, and n = 5, then:

    pos = ( 1 * 5 ) + 2;
    Item * it =  m[ pos ];
    

    Also, instead of using plain pointers, you can use smart pointers, such as auto_ptr (obsolete) and unique_ptr, which are more or less similar: once they are destroyed, they destroy the object they are pointing to.

    auto_ptr<Item> m = new auto_ptr<Item>[ n * n ];
    

    The only drawback is that now you need to call get() in order to obtain the pointer.

    pos = ( 1 * 5 ) + 2;
    Item * it =  m[ pos ].get();
    

    Here you have a class that summarizes all of this:

    class ItemsSquaredMatrix {
    public:
        ItemsSquaredMatrix(unsigned int i): size( i )
            { m = new std::auto_ptr<Item>[ size * size ]; }
        ~ItemsSquaredMatrix()
            { delete[] m; }
    
        Item * get(unsigned int row, unsigned int col)
            { return m[ translate( row, col ) ].get(); }
        const Item * get(unsigned int row, unsigned int col) const
            { return m[ translate( row, col ) ].get(); }
    
        void set(unsigned int row, unsigned int col, Item * it)
            { m[ translate( row, col ) ].reset( it ); }
    
        unsigned int translate(unsigned int row, unsigned int col) const
            { return ( ( row * size ) + col ); }
    
    private:
        unsigned int size;
        std::auto_ptr<Item> * m;
    };
    

    Now you only have to create the class Item. But if you created a specific class, then you’d have to duplicate ItemsSquaredMatrix for each new piece of data. In C++ there is a specific solution for this, involving the transformation of the class above in a template (hint: vector<> is a template). Since you are a beginner, it will be simpler to have Item as an abstract class:

    class Item {
    public:
        // more things...
        virtual std::string toString() const = 0;
    };
    

    And derive all the data classes you will create from them. Remember to do a cast, though…

    As you can see, there are a lot of open questions, and more questions will raise as you keep unveliling things. Enjoy!

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the correct way to implement a constructor in android? It seems that
I am wondering if there is a correct way to implement the anchor tag
What's the correct way to set a flash message for the current view but
Would the following be the correct way to implement a fairly straightforward thread-safe logging
I was wondering what is the correct way to implement multiple choices in django-models.
I'm having trouble on the correct way to implement this. What i want to
What is the correct way to implement and architect a command line tool as
Possible Duplicate: Efficient way to implement singleton pattern in Java I was reading this
Ok, I think its possible I've misunderstood the correct way to implement an external
I'm scratching my head to find the correct way to implement a dynamic sliding/expanding

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.