有可能创建一个结构的二维矢量吗

Is it possible to create a 2D vector of structs?

本文关键字:二维 结构 创建 一个 有可能      更新时间:2023-10-16

到目前为止,我已经设置了一个int的2D向量(下面的代码正在工作),但我真正想要的是一个名为"small_tile"的结构的2D向量数组。

在这里,我想用txt文件中的数据填充结构的纹理int。稍后,我还将填充其他数据值"tile_x,tile_y,r,c"。

我希望我的问题是明确的

#include <iostream>
#include <fstream>
#include <vector>
struct smallTile
{
    int tile_x;
    int tile_y;
    int r;
    int c;
    int texture; // 0=grass, 1=sand, 2=... (get this data from txt file)
};
int main()
{
    int SMALL_TILE_VECTOR_ROWS = 5;
    int SMALL_TILE_VECTOR_COLUMNS = 6;
    //Create vector array: 5x6 containing nothing:
    std::vector<std::vector<int> > vvint(SMALL_TILE_VECTOR_ROWS, std::vector<int>(SMALL_TILE_VECTOR_COLUMNS));
    //Fill vector with file information. 
    std::ifstream file ("levelMap.txt");
    for(int r = 0; r < vvint.size(); r++)
    {
        for (int c = 0; c < vvint.at(0).size(); c++)
        {
            file >> vvint[r][c];
        }
    }
    file.close();
    //cout out the data inside the vector array so I can see it's working:
    for(int r = 0; r < vvint.size(); r++)
    {
        for(int c = 0; c < vvint.at(0).size(); c++)
        {
            std::cout<< vvint[r][c] << " ";
        }
        std::cout<< "n";
    }
    return 0;
}
std::vector <smallTile> smalltiles;

//问题出在哪里?