gpt4 book ai didi

c++ - 是否可以创建结构的二维 vector ?

转载 作者:行者123 更新时间:2023-11-28 06:20:41 25 4
gpt4 key购买 nike

到目前为止,我已经设置了一个 2D 整数 vector (下面的代码有效),但我真正想要的是一个名为“small_tile”的结构的 2D vector 数组。

在这里,我想用 txt 文件中的数据填充结构的纹理整数。稍后我还将填充其他数据值“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;

//问题是什么?

关于c++ - 是否可以创建结构的二维 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29359609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com