2010-07-04 121 views
3

我一直在试图创建一个游戏的矢量2D阵列。这是我正在做的一个例子。创建一个矢量的2D阵列

struct TILE { 
    int a; 
    char b; 
    bool c; 
}; 

TILE temp_tile; 

std::vector<TILE> temp_vec_tile; 
std::vector<std::vector<TILE>> tile; 


for (int x = 0; x < 10; x++) { 
    for (int y = 0; y < 10; y++) { 

    temp_tile.a = x; 
    temp_tile.b = "a"; 
    temp_tile.c = false;; 

    temp_vec_tile.push_back(temp_tile); 
    } 

    tile.push_back(temp_vec_tile); 
} 

// Why does this not work? 
int x = tile[3][5].a; 

注意:我不想为此使用Boost。

谢谢

+0

什么是不工作? – casablanca 2010-07-04 02:22:02

+0

Brian R. Bondy已经解决了这个问题 – user382909 2010-07-04 02:28:46

回答

0

你不是每次都清除内部向量。可能你想要的是将内部向量声明放在第一个for循环中。

+0

谢谢,这个技巧! – user382909 2010-07-04 02:28:08