到目前爲止,我已經建立了一個二維向量(下面的代碼,工作),但我真正想要的是一個名爲'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;
}
你試過了嗎? – chris 2015-03-31 02:34:04