简体   繁体   English

C ++ 2D矢量地图

[英]C++ 2D vector maps

First of all, I know this question has been asked alot, but I need to clarify and understand some things. 首先,我知道这个问题已经问了很多,但是我需要澄清和理解一些事情。 Also, I am still learning c++, currently from a book and tutorials, so don't be harsh :) 另外,我仍在学习c ++,目前正在从一本书和教程中学习,所以不要苛刻:)

How can I have a 2D map for a platformer defined in code and modified? 如何为用代码定义和修改的平台游戏制作2D地图? I understand basic multidimentional arrays, but I really can't use them because the size of my map will be changing alot. 我了解基本的多维数组,但是我真的不能使用它们,因为地图的大小将不断变化。 I can use a vector of vectors, but from what I have heard, this isn't a rectangular grid any more, it is more like... 我可以使用向量的向量,但是据我所知,这不再是矩形网格,更像是...

x x x x x x x x x x   
x       x . . . . .
x       x . . . . .
x x x x x x x x x x

Where x is a tile, space is an air tile, and a dot is not allocated. 其中x是图块,空间是空气图块,并且未分配点。 Not a grid! 不是网格!

I am also confused on how pointers of arrays work. 我也对数组指针的工作方式感到困惑。

Sorry for being abit of a noob here, I am still trying to work things out. 很抱歉在这里成为菜鸟,我仍在努力解决问题。

Any help would be greatly apprieciated! 任何帮助将不胜感激!

I would definitely go for a vector of vectors if you need a simple resizable matrix class. 如果您需要一个简单的可调整大小的矩阵类,我肯定会选择向量的向量。 You can always wrap this into a Matrix class on your own (perhaps by templating the dimensions/types) 您总是可以自己将其包装到Matrix类中(也许通过模板化尺寸/类型)

struct Tile {
    int whatever = 0;
};

int main() {
    std::vector<std::vector<Tile>> m;
    m.resize(10);
    for(auto& v : m)
        v.resize(10);

    // Print a 10x10 0-initialized matrix
    for(auto& v : m) {
        for(auto& elements : v)
            std::cout << elements.whatever << " ";
        std::cout << std::endl;
    }
}

Live example 现场例子

Memory in a vector is contiguously allocated, there are no "holes". 向量中的内存是连续分配的,没有“空洞”。 Anyway you'll have to keep the dimensions of the inner vectors in sync if you resize one. 无论如何,如果您要调整内部向量的大小,则必须使其保持同步。

If you believe this is too much work for what you have in mind, go for Boost BLAS capabilities with Matrix . 如果您认为这对您的想法来说工作量太大,请使用Matrix的 Boost BLAS功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM